are unions/sum types a thing yet?
# announcements
b
are unions/sum types a thing yet?
🚫 2
c
I don’t think Kotlin should support union types, they really go against the idea of type safety (with them, a single object could be one of several disparate types; ambiguity ensues). You might check out Arrow, which has many such asbtractions, such as
Either
https://arrow-kt.io/docs/arrow/core/either/ which do not introduce ambiguity to the type system
1
For javascript interop, you have the
dynamic
type already for cases where JS objects have that union type, and you can cast/convert that to the Kotlin type as appropriate
s
With sealed classes and smart-casting, you can create your own sum-types, like the
Either
from Arrow.
b
yeah, sealed classes don't work with existing types do they?
s
No, that is correct. You’d have to do
Either<Int, String>
or something.
b
I see, never thought of using the type this way
map & co won't make sense ofc but that doesn't matter in this case
s
Copy code
sealed class Either<out L, out R>
class Left<out L>(val left: L) : Either<L, Nothing>()  { ... }
class Right<out R>(val right: R): Either<Nothing, R> { ... }
And add some extension functions to ‘map’, ‘flatMap’, etc.
g
You could create an issue for it, and if receive enough votes could be interesting. I’d vote for it
p
I really would love to see union types
I have an implementation similar to adapter delegates by hannes Dorfmann and it would greatly benefit from union types
k
This is the discussions post about it: https://discuss.kotlinlang.org/t/union-types/77, and this is the Shelved issue: https://youtrack.jetbrains.com/issue/KT-13108
y
Nullable types in Kotlin are a form of union types, aren't they?
k
Sure, a very limited form.
r