Union types taking shape in the IDE <https://kotli...
# arrow
j
Is it significant that
help
returns
String?
, not
String
? I ask, because I noticed that name and hash aren't optional, and the union doesn't appear to allow for null of either type. Does the compiler not know that returning null from
help
isn't possible? Is it possible?
r
Hi @julian, that is just a currently unfinished feature that is pattern matching over unions is not yet exaustive but it will be soon. That is why I used nullable types otherwise is just
String
. To your second question yes once that was expressed as pattern matching the compiler knows unless the union includes nullable types in which case the union is of type:
Copy code
A | B | C | null
for example that is isomorphic to:
A? | B? | C?
since a union is a choice null, is expanded as a case and all others are normalized to non nullables
j
Cool. Thanks! @raulraja
👍 1
b
So I understand
A | B | C | null
syntax but including
null
as a type looks weird to me, because nullable types in Kotlin suggest that
null
is a value, not a type
Also how would that work with
String | Int | Double?
or (worse)
String? | Int | Double?
r
when a nullable type appears in a union that is the same as if all the types were not null and null was one of the possible values
that is because A? is the same as A | null
All your unions above are isomorphic or the same as
String | Int | Double | null
a union can always return null for a typed value that is not the actual value in the union
A? if it did not exist explictly in Kotlin it would be A | null
b
I dunno, it just strikes me as super-weird because strictly speaking I think
A | B | C | null
is isomorphic to
A | B | C | Unit
I am having trouble imagining a case in new code (because union types can only be added to new code) where
null
would be preferable to using
Unit
r
null is both a type and a value
the safe usage in kotlin turns it into a singleton type
is also isomorfic to unit
you can use null for control flow or anything so it must have a type. The issue is that the type is just the simplification of A | Null