Search code examples
scalashapeless

Flattening a sealed case class hierarchy


Suppose I have a sealed case class hierarchy like the following:

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
  • Is it possible to automatically convert expressions such as Plus(1,Plus(2,3)) into a HList of HLists?
  • Will the conversion work even inside some function f(e: Expr), i.e. when the specific structure of e is not known at compile time?

Solution

  • It turns out that this has already been answered on SO here and that there's a corresponding example in the Shapeless distribution as of shapeless-2.1.0-SNAPSHOT.