如何在scala中显示一个HList的类型无形

时间:2017-05-03 12:16:21

标签: scala shapeless

如何将HList的类型作为String获取,以便我可以打印它。例如"Int :: Long :: String :: HNil"

val gen = Generic[?]
val typeString: String = ???
println("The type is " + typeString)

我知道它的字符串非常有用,通常你想要gen.Repr

的类型

1 个答案:

答案 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