This is issue #1551 in the dotty repo. The main problem with singleton types in union types is this:
If we allow them, then the least upper bound of x.type
and y.type
is x.type | y.type
. Hence, in an
if-then-else
if (cond) x else y
this would be the type that’s computed for it. For nested if-then-elses or multiple branch match
expressions the size of the type would grow linearly. This is simply not practical. It’s unclear what to do about it, since (a) the concept of least upper bounds is central to many aspects of Scala type inference, and (b) the concept is well-defined; it is what the name says it is, so one cannot mess with it and give it another meaning.
On the other hand, if we disallow unions over singletons then the least upper bound of x.type
and y.type
is simply the common underlying type of x
and y
, which is what one would expect it to be. Simple and effective. That’s why we opted for it. If there is another solution that is more general, but at the same time stays simple and clearly demonstrates that there are no issues with other type inference aspects we can still adopt it. But so far, no such solution is known.