I would like to condense my evaluator thanks to a custom generic unapply function, which evaluates the argument and returns the value if it succeeds.
But this fails with the error error: not found: type Eval
Any way of achieving this? I have looked at typetags, implicit conversions for unapply methods but I do not see how I could integrate them in this problem. How to correctly define Eval?
object Test { case class Context() trait Expr trait Literal[T] extends Expr{ def value : T } case class IntLiteral(value: Int) extends Literal[Int] case class StringLiteral(value: Int) extends Literal[Int] case class Plus(e: Expr, f: Expr) extends Expr object Eval { // Here I want the magic unapply to evaluate the expression. def unapply[T](e: Expr)(implicit gctx: Context): Option[T] = { eval(e) match { case e: Literal[T] => Some(e.value) case _ => None } } } def eval(e: Expr)(implicit c: Context): Expr = e match { case Plus(Eval[Int](i), Eval[Int](j)) => IntLiteral(i+j) // Fails here. case IntLiteral(i) => e case StringLiteral(s) => e } eval(Plus(Plus(IntLiteral(1),IntLiteral(2)),IntLiteral(3)))(Context()) }