简体   繁体   中英

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

I'm unable to implement shapeless.Generic with scala type parameterized functions. In the following piece of code, I am getting error "could not find implicit value for parameter 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. If you don't understand all the words here just yet, read more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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