简体   繁体   中英

how to show the type of a HList in scala shapeless

How do I get the type of a HList as a String so I can print it. eg "Int :: Long :: String :: HNil"

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

I know the String of it isnt very useful and usually you want the type from gen.Repr

Use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM