Programming

9 thoughts
last posted July 1, 2015, 3:08 p.m.

1 later thought

0

Kotlin now has sealed classes. I think they primarily resemble the concept of "tagged union"s. Rust has a similar elegant construct in form of enums.

sealed class Type () {
    class Named(val name: String): Type()
    class Nested {
        class Function(val param: Type, 
                       val result: Type): Type()
    }
    object Top: Type()
}
when (type) {
    is Named -> println(name)
    is Nested.Function -> println("$param -> $result")
    // Alternatively, we can omit is here
    is Top -> println("TOP")
}

7 earlier thoughts