【问题标题】:Flattening a sealed case class hierarchy展平密封案例类层次结构
【发布时间】: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. 因为Exprsealed trait,那么编译器在编译时就知道所有子类,例如穷举匹配。我不明白这个评论/问题 - 你能解释一下吗?
  • 我的意思是:从 f 内部,它是否被 f(Plus(1,x)) 或 f(Times(a,b)) 调用是不明确的 - 这是扁平化的障碍吗从 f 内部将表达式转换为 HList?

标签: scala shapeless


【解决方案1】:

事实证明,SO here 已经回答了这个问题,并且从 shapeless-2.1.0-SNAPSHOT 开始,Shapeless 分布中有一个对应的 example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多