繁体   English   中英

Scala类型参数化,Shapeless-找不到参数Generic的隐式值

[英]Scala type parameterization, Shapeless - could not find implicit value for parameter Generic

我无法实现shapeless.Generic与Scala类型的参数化函数。 在以下代码中,我收到错误消息“找不到参数gen的隐式值:shapeless.Generic [T]”。

  def foo[T](instance: T) = {
    val gen = shapeless.Generic[T]  //getting error in this line!!!
    val values = gen.to(instance)
    println(values)
  }
  case class Bar(x:String, y:String)
  var bar = Bar("a","b")
  foo(bar) 

我有什么想念的吗?

  def foo[T, HL <: HList](instance: T)(
    implicit gen: Generic.Aux[T, HL]
  ) = {
    val values = gen to instance
    println(values)
  }

case class Bar(x: String, y: String)

通常,您需要使用Aux模式,泛型是宏实现的,但是会产生一个暴露为抽象类型成员的任意类型。 如果您现在还听不懂所有单词,请在此处阅读更多内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM