Skip to content

Instantly share code, notes, and snippets.

@belyaev-mikhail
Last active July 5, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belyaev-mikhail/8988c4ce00d0718f11d84ca11397550a to your computer and use it in GitHub Desktop.
Save belyaev-mikhail/8988c4ce00d0718f11d84ca11397550a to your computer and use it in GitHub Desktop.
// 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