ceylon/ceylon-spec — Issue #144: Figure out exactly in what cases we should reduce algebraic types to unions
Author: gavinking
Created: 2012-01-22 05:59:12+00:00
State: open
Labels: improvement

For various reasons, we definitely don't want to go around canonicalizing every occurrence of `Boolean` to `truth|falsity`, but there are certain cases where I think we should make use of the equivalence of an algebraic type to a union.

Consider:

interface Interface of Class1 | Class2 {}
class Class1() satisfies Interface {}
class Class2() satisfies Interface {}

Interface i = Class1();
if (is String i) {
print(i);
}

Currently `i` has the type `Interface&String` inside the body of `if`. But it's clear that in fact the type is provably equivalent to `Bottom`. The `switch` statement already does some of this kind of reasoning, but `if (is/exists/nonempty ..)` doesn't. And even `switch` is currently deficient in it's handling of intersection types.

Similarly, I think this code should be accepted by the typechecker:

Interface i = Class1();
Class1|Class2 c = i;
Sequence s = {i};