简体   繁体   English

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

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

I'm unable to implement shapeless.Generic with scala type parameterized functions. 我无法实现shapeless.Generic与Scala类型的参数化函数。 In the following piece of code, I am getting error "could not find implicit value for parameter gen: shapeless.Generic[T]". 在以下代码中,我收到错误消息“找不到参数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) 

Is there anything I am missing? 我有什么想念的吗?

  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)

You need to use the Aux pattern usually, generics are macro materialised but produce an arbitrary type that's exposed as an abstract type member. 通常,您需要使用Aux模式,泛型是宏实现的,但是会产生一个暴露为抽象类型成员的任意类型。 If you don't understand all the words here just yet, read more here . 如果您现在还听不懂所有单词,请在此处阅读更多内容。

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

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