https://kotlinlang.org logo
Title
a

Ayfri

08/08/2022, 4:51 PM
It would be great to have union types just for external declarations in K/JS, so it's a lot easier to create typings for a TypeScript library. I think it would be a pretty easy task to implement it just in that specific context so this is why I post a message specifically for this
d

dmitriy.novozhilov

08/08/2022, 6:24 PM
It won't be easy, because it means full support of union types in type system, which is main blocker from design and implementation sides
a

Ayfri

08/08/2022, 6:26 PM
Ah, but doesn't they exist in a way when using multiple return types in a
when
?
d

dmitriy.novozhilov

08/08/2022, 6:35 PM
No.
when
expressions are nothing more than
select
function from inference point of view
when {
  ... -> x
  ... -> y
  ... -> z
}
// same as
fun <K> select(vararg value: K): K

select(x, y, z)
Boolean & String
is an intersection type, not union
interface A
interface B
interface C : A, B
interface D : A, B

fun test(c: C, d: D) {
    val x = select(c, d)
    // type of x is CommonSuperType(C, D) = A & B
}
a

Ayfri

08/09/2022, 5:37 AM
Ah okay I see, thanks for the information :)