guys, is there any discussion or explanation about...
# announcements
e
guys, is there any discussion or explanation about, why no union/intersection types (like in typescript) in kotlin? I don’t think, that it’s a must have feature, but I think this topic is quite interesting and was already discussed
j
It would be useful
k
d
Isn't a
sealed class
what you are after? These are very powerful and flexible, and can behave semantically like a `union`ed type in C (even if the underlying memory layout/usage doesn't follow suit).
k
Well for example this isn't really possible: (made-up syntax)
typedef Value = String | Int
, you can emulate it with container sealed classes but that's a lot of boilerplate.
d
Ah, ok, yes it'd be:
Copy code
sealed class Value {
    data class String( value: String ) : Value()
    data class Int( value: Int ) : Value()
}
Or some variant using generics
but either way, as you say, a tonne more boilerplate than your pseudocode.
Your proposal looks like it'd be a headache for the type system though; not sure how that'd work, but maybe I just need to be enlightened 🙂
k
This is not my proposal, I can't say I've ever needed them. There are a ton of languages that do this, from Haskell to C++.