Last active
August 29, 2015 14:03
-
-
Save privateblue/59afd8b3e8a958abd060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Value[T] { | |
def eval(e: T): Int | |
} | |
def value[T: Value](e: T) = implicitly[Value[T]].eval(e) | |
sealed trait Expr | |
case class Num(n: Int) extends Expr | |
object Num { | |
implicit val numValue = | |
new Value[Num] { | |
def eval(e: Num) = e.n | |
} | |
} | |
case class Add[T <: Expr, U: Value](e1: T, e2: U) extends Expr | |
object Add { | |
implicit def addValue[T <: Expr : Value, U <: Expr : Value] = | |
new Value[Add[T, U]] { | |
def eval(e: Add[T, U]) = value(e.e1) + value(e.e2) | |
} | |
} | |
case class Sub[T <: Expr, U <: Expr](e1: T, e2: U) extends Expr | |
object Sub { | |
implicit def subValue[T <: Expr : Value, U <: Expr : Value] = | |
new Value[Sub[T, U]] { | |
def eval(e: Sub[T, U]) = value(e.e1) - value(e.e2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment