Ra Ka
Ra Ka

Reputation: 3055

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?

Upvotes: 2

Views: 1573

Answers (1)

flavian
flavian

Reputation: 28511

  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.

Upvotes: 3

Related Questions