I'm trying to parametrize a method that needs to work on a generic type
A
LabelledGeneric
case class Foo(bar: String, baz: Boolean)
def params[A](a: A) = {
val lbl = LabelledGeneric[A]
val keys = Keys[lbl.Repr].apply
...
}
val myThingy = params(Foo)
A
type A is not a class or trait
LabelledGeneric
def params[A](a: A)(implicit lbl: LabelledGeneric[A]) = {
val keys = Keys[lbl.Repr].apply
...
}
Repr
HList
type arguments [lbl.Repr] do not conform to method apply's type parameter bounds [L <: shapeless.HList]
def params[A, Repr <: HList](a: A)(implicit lbl: LabelledGeneric.Aux[A, Repr]) = {
val keys = Keys[lbl.Repr].apply
...
}
Repr
HList
Keys
could not find implicit value for parameter values: shapeless.ops.record.Values[lbl.Repr]
def params[A, Repr <: HList](a: A)(implicit
lbl: LabelledGeneric.Aux[A, Repr],
kk: Keys[Repr]
) = {
val keys = kk.apply
...
}
could not find implicit value for parameter lbl: shapeless.LabelledGeneric.Aux[example.Main.Foo.type,Repr]
[error] params(Foo)
val lbl = LabelledGeneric[Foo]
val keys = Keys[lbl.Repr].apply
// no problem
The last variant with everything computed implicitly works for me,
scala> import shapeless._, ops.record._
import shapeless._
import ops.record._
scala> :paste
// Entering paste mode (ctrl-D to finish)
def params[A, Repr <: HList](a: A)
(implicit lbl: LabelledGeneric.Aux[A, Repr], kk: Keys[Repr]) = {
val keys = kk.apply
keys
}
// Exiting paste mode, now interpreting.
params: ...
scala> case class Foo(bar: String, baz: Boolean)
defined class Foo
scala> params(foo)
res0: ... = 'bar :: 'baz :: HNil
(result types elided for readability).