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 enum
s.
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")
}