Created
September 13, 2013 06:58
-
-
Save takkkun/6547465 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 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