Created
January 13, 2020 08:42
-
-
Save elizarov/b5400f827616090811ebb348acd4c426 to your computer and use it in GitHub Desktop.
This file contains 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 Expression<A> { | |
abstract val value: A | |
} | |
data class Num(override val value: Int) : Expression<Int>() | |
data class Bool(override val value: Boolean) : Expression<Boolean>() | |
data class Add(val a: Expression<Int>, val b: Expression<Int>) : Expression<Int>() { | |
override val value: Int get() = a.value + b.value | |
} | |
data class Equals<A>(val first: Expression<A>, val second: Expression<A>) : Expression<Boolean>() { | |
override val value: Boolean get() = first.value == second.value | |
} | |
fun main() { | |
val exp = Equals( | |
Equals( | |
Num(3), | |
Add( | |
Num(1), | |
Num(2) | |
) | |
), | |
Bool(true) | |
) | |
println(exp.value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment