https://kotlinlang.org logo
Title
e

evkaky

09/11/2017, 4:40 PM
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

janvladimirmostert

09/11/2017, 4:57 PM
It would be useful
k

karelpeeters

09/11/2017, 6:03 PM
d

darkmoon_uk

09/24/2017, 12:02 PM
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

karelpeeters

09/24/2017, 12:12 PM
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

darkmoon_uk

09/24/2017, 12:33 PM
Ah, ok, yes it'd be:
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

karelpeeters

09/24/2017, 1:23 PM
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++.