3
votes

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())
}
1
Very interested in this question too...Sergey Kostrukov

1 Answers

2
votes

Eval[Int](...) is simply not a legal pattern, so you can't get this syntax. You can make Eval itself generic and create instances for types you want:

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

  case class Eval[T]() {

    def unapply(e: Expr)(implicit gctx: Context): Option[T] = {
      eval(e) match {
        case e: Literal[T] => Some(e.value)
        case _             => None
      }
    }
  }

  val IntEval = Eval[Int]()

  def eval(e: Expr)(implicit c: Context): Expr = e match {
    case Plus(IntEval(i), IntEval(j)) => IntLiteral(i + j) 

    case IntLiteral(i)                => e
    case StringLiteral(s)             => e
  }

  println(eval(Plus(Plus(IntLiteral(1), IntLiteral(2)), IntLiteral(3)))(Context()))
}

but note that this doesn't check the type of the literals! If you want this as well, you need ClassTag and this only allows matching : T, not : Literal[T]:

object Test {
  case class Context()

  trait Expr
  case class Literal[T](value: T) extends Expr // or case class Literal(value: Any)
  case class Plus(e: Expr, f: Expr) extends Expr

  case class Eval[T]()(implicit tag: scala.reflect.ClassTag[T]) {

    def unapply(e: Expr)(implicit gctx: Context): Option[T] = {
      eval(e) match {
        case Literal(value: T) => Some(value)
        case _             => None
      }
    }
  }

  val IntEval = Eval[Int]()

  def eval(e: Expr)(implicit c: Context): Expr = e match {
    case Plus(IntEval(i), IntEval(j)) => Literal(i + j)

    case e: Literal[_]                => e
  }

  println(eval(Plus(Plus(Literal(1), Literal(2)), Literal(3)))(Context()))
}