Intersection types in patterns

Brian Goetz brian.goetz at oracle.com
Sat Oct 20 14:43:21 UTC 2018


> It seems that sometimes it could be useful to have intersection types
> in patterns.

Yes, its worth thinking about.  

> Another interesting case is when LHO of matching operator is not
> assignable from pattern type. Like `if (getFoo() instanceof Bar bar) {
> bar.bar(); bar.foo(); }`. 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.

Another way to get here is: let the user reuse the name:

    Foo f;
    if (f instanceof Bar f) { … refine type of f to Foo&Bar … }

You could call this “manual flow typing”, where rather than magically rewriting the type of f, we allow you to explicitly refine the type of f.  




More information about the amber-spec-experts mailing list