Multiple types like in TypeScript

Kotlin allows multiple types already, when using null.

fun parseDigits(s: String):Integer? expects the programmer to test for null or an Integer type.

I suggest implementing an alternative syntax as soon as possible
(so later you can kill the meaningless questionmark)

fun parseDigits(s: String): Integer|null that results in an Integer or null value

So like in TypeScript multiple return types can be specified.

fun parseDigit(s: String): Integer|Float|BigSuperNumber|null

Consequently useable in all type definitions:

var list: List<Integer|String|null>() for a List containing Integer, Float or String elements only (so no need for casting elements in an object list).

fun something(cnt:Integer, arg:Float|Integer):Float|null

BONUS:

This will prevent the use of sealed classes in many cases.

IDEA:

Can be used for returning checked exceptions like:
fun parseNumber(s: String):Integer|Float|BadNumberFormatException

Has been discussed profusely here:

For what’s it worth, I really like the ? since something|null is where this is really useful

If I think about union types, I think about a “union memory space” where the value is stored in a memory block with the size of the maximum sized element. Was that implemented C or even Pascal (?), I did not remember.

The Multiple types like in TypeScript are just generics helping writing cleaner code that was compiled to Object-Type. The compiler will force the programmer to check each instance of the returned MultiType. In combination with the fabulous smart casting this will create great code.

I will read your mentioned thread, thank you.

It might be useful for you to see how Rust has done unions as well; they’re quite similar to sealed classes and use exactly the method you describe in their implementation.

The types in typescript are just generics.

I did not like the creation of new objects just for holding object variants (Rust uses that mechanism too, I think). These holder objects are a large overhead (memory allocation etc.) and completely unnecessary then using Multi-Types like in typescript.

The typescript (transpiler) types are just used for forcing the programmer to test all declared instances (on return values), the code itself handles objects only (the javascript language itself is not changed).

So easy to implement and fully compatible to Java.

Future scala 3 version (dotty) also has union type.
Rust is effectivly different, as C union type, it’s just here to help to save memory.
In scala 3 or typescript, it helps to better model data & functions.