Last active
July 9, 2017 09:48
-
-
Save nomisRev/4f4fcf8430fc4ffcbcf4d20e43925c89 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
sealed class MathExpression { | |
fun eval(): Int = when (this) { | |
is Addition -> left.eval() + right.eval() | |
is Subtraction -> left.eval() - right.eval() | |
is Division -> left.eval() / right.eval() | |
is Number -> value | |
} | |
} | |
data class Addition(val left: MathExpression, val right: MathExpression) : MathExpression() | |
data class Subtraction(val left: MathExpression, val right: MathExpression) : MathExpression() | |
data class Division(val left: MathExpression, val right: MathExpression) : MathExpression() | |
data class Number(val value: Int) : MathExpression() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment