【问题标题】:how to show the type of a HList in scala shapeless如何在 scala shapeless 中显示 HList 的类型
【发布时间】:2017-05-03 12:16:21
【问题描述】:

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

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

我知道它的字符串不是很有用,通常你想要来自gen.Repr的类型

【问题讨论】:

标签: scala shapeless


【解决方案1】:

使用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

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多