Intersection types in patterns

Tagir Valeev amaembo at
Sat Oct 20 05:58:24 UTC 2018


It seems that sometimes it could be useful to have intersection types
in patterns. Assuming
interface Foo { void foo(); }
interface Bar { void bar(); }

one might want to match `obj instanceof (Foo & Bar) foobar` to be able
to call both `foo()` and `bar()`. After all we can now declare `var
foobar = (Foo & Bar) obj`.

Another interesting case is when LHO of matching operator is not
assignable from pattern type. Like `if (getFoo() instanceof Bar bar) {;; }`. We know that getFoo() returns Foo, thus bar
is Foo as well. Probably it's type could be narrowed to the (Foo &
Bar) intersection automatically.

I'm not sure whether there are many usecases for this, but probably it
worth exploring.

With best regards,
Tagir Valeev.

More information about the amber-spec-experts mailing list