Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created January 13, 2020 08:42
Show Gist options
  • Save elizarov/b5400f827616090811ebb348acd4c426 to your computer and use it in GitHub Desktop.
Save elizarov/b5400f827616090811ebb348acd4c426 to your computer and use it in GitHub Desktop.
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