3
커스텀 제네릭 unapply 함수에 내 평가자 감사를 응축하고 싶습니다.이 함수는 인수를 평가하고 성공하면 값을 반환합니다.스칼라 커스텀 제네릭으로 적용 취소
하지만이 오류 error: not found: type Eval
으로이 달성의 어떤 방법을 실패? 나는 typetags, implicit conversions for unapply methods을 보았습니다. 그러나 나는이 문제에서 어떻게 그들을 통합 할 수 있는지 보지 못합니다. 정확하게 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()) }
매우 관심이 ... –