whooooaaaa I have never seen this before. Is this...
# announcements
h
whooooaaaa I have never seen this before. Is this recent? Does this mean Kotlin might be getting union types in the future? That and default type parameters are some of my most desired features for the language. It would help so much with writing generic functions for differing number types, collections types, and more. The syntax of the hint is not valid syntax however
d
Kotlin has had union types for a long time (if not from the start). They are just not denotable (i.e. you can't write them down).
h
So could I write a single method that will accept only
Int
and
Double
and have access to arithmetic operators? I'm almost certain the answer is no way. But I'd be thrilled if there were
it gets tedious to write stuff like
Copy code
operator fun plus(o: Int) = ...
operator fun plus(o: Double) = ...
Copy code
fun DoubleArray.toVector() = ...
fun IntArray.toVector() = ...
fun Array<Double>.toVector() = ...
fun Array<Int>.toVector() = ...
fun List<Double>.toVector() = ...
fun List<Int>.toVector() = ...
and so on...
k
This isn't a union type, it's a intersection of types.
👆 5
s
Arrow is actively working on a compiler-plugin and it will have the ability to allow union types. https://github.com/arrow-kt/arrow-meta/pull/64
😍 2
m
The notation you’re seeing in the hint comes maybe from the JVM (just like platform types). As they pointed out it’s not a union type but an intersection, and you can use that for generic bounds: https://kotlinlang.org/docs/reference/generics.html#upper-bounds