Last active
July 5, 2019 09:55
-
-
Save belyaev-mikhail/8988c4ce00d0718f11d84ca11397550a 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
// sum type (typical Kotlin implementation) | |
sealed class Expr | |
data class Var(val name: String): Expr() | |
data class Constant(val value: Int): Expr() | |
object Empty: Expr() | |
// union type (hypothetical) | |
data class Var(val name: String) | |
data class Constant(val value: Int) | |
object Empty | |
typealias Expr = Var | Constant | Empty | |
// should work for both | |
fun choice(e: Expr) = when(e) { | |
Empty -> ... | |
is Var -> ... | |
is Constant -> ... | |
// no else needed here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment