【发布时间】:2015-12-18 15:50:54
【问题描述】:
假设我有一个密封的案例类层次结构,如下所示:
sealed trait Expr
case class Const(val: Double) extends Expr
case class Plus(x: Expr, y: Expr) extends Expr
case class Times(x: Expr, y: Expr) extends Expr
- 是否可以将
Plus(1,Plus(2,3))等表达式自动转换为HLists的HList? - 即使在某些函数
f(e: Expr)内,转换是否也能工作,即在编译时不知道 e 的具体结构时?
【问题讨论】:
-
... when the specific structure of e is not known at compile time.因为Expr是sealed trait,那么编译器在编译时就知道所有子类,例如穷举匹配。我不明白这个评论/问题 - 你能解释一下吗? -
我的意思是:从 f 内部,它是否被 f(Plus(1,x)) 或 f(Times(a,b)) 调用是不明确的 - 这是扁平化的障碍吗从 f 内部将表达式转换为 HList?