Skip to content

Instantly share code, notes, and snippets.

@takkkun
Created September 13, 2013 06:58
Show Gist options
  • Select an option

  • Save takkkun/6547465 to your computer and use it in GitHub Desktop.

Select an option

Save takkkun/6547465 to your computer and use it in GitHub Desktop.
trait Expression {
def +(right: Expression) = Addition(this, right)
def *(right: Expression) = Multiplication(this, right)
def **(right: Expression) = Exponential(this, right)
}
object Expression {
implicit def intToConst(value: Int) = Const(value)
}
case class Var(name: String) extends Expression
case class Const(value: Int) extends Expression
case class Addition(left: Expression, right: Expression) extends Expression
case class Multiplication(left: Expression, right: Expression) extends Expression
case class Exponential(left: Expression, right: Expression) extends Expression
object Main {
def main(string: Array[String]) {
import Expression._
val x = Var("x")
val y = x ** 2 + x * 4
println(y)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment