Under -Yexplicit-nulls, Scala currently employs a form of flow typing to enable code like this
def orZero(x: Int | Null): Int =
if x != null then x else 0
In explicit nulls mode, the compiler determines that it’s OK to use x, which is of type Int | Null, in a position where Int is required, because the appropriate null check has been performed. But when -Yexplicit-nulls is not given, this fails to compile:
-- [E007] Type Mismatch Error: -------------------------------------------------
2 | if x != null then x else 0
| ^
| Found: (x : Int | Null)
| Required: Int
|
Why wouldn’t we always enable this?