简体   繁体   English

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

[英]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. 如何获取HList的类型作为字符串,以便可以打印它。 eg "Int :: Long :: String :: HNil" 例如"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 我知道它的字符串不是很有用,通常您需要gen.Repr的类型

Use shapeless.Typeable : 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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