2
votes

I'd like to do the following with Shapeless 2.0.0:

  def freeze[T]( o:T ) = {
   val gen = Generic[T]
   gen.to(o)
  }

This gives me an error saying that T is not a case class or trait. Is there any way to do this? (I can do val gen = Generic[Foo]. That's fine, but what if I need to be able to build a Generic from something not known at compile-time?)

1

1 Answers

4
votes

There's not really anything interesting you can do with a plain old T—most operations in Shapeless require you to provide some kind of evidence about the type in the form of implicit parameters, like this:

import shapeless.Generic

def freeze[T](o: T)(implicit gen: Generic[T]) = {
  gen.to(o)
}

And then:

scala> case class Foo(i: Int)
defined class Foo

scala> freeze(Foo(10))
res0: shapeless.::[Int,shapeless.HNil] = 10 :: HNil

The only time Generic[T] will work as a value is when the compiler knows concretely what T is.