如何将HList的类型作为String获取,以便我可以打印它。例如"Int :: Long :: String :: HNil"
val gen = Generic[?]
val typeString: String = ???
println("The type is " + typeString)
我知道它的字符串非常有用,通常你想要gen.Repr
答案 0 :(得分:5)
使用shapeless.Typeable
:
scala> import shapeless._
import shapeless._
scala> case class A(i: Int, s: String)
defined class A
scala> val gen = Generic[A]
gen: shapeless.Generic[A]{type Repr =
shapeless.::[Int,shapeless.::[String,shapeless.HNil]]} =
anon$macro$14$1@56639061
scala> println(Typeable[gen.Repr].describe)
Int :: String :: HNil