Two new draft pattern matching JEPs
Brian Goetz
brian.goetz at oracle.com
Thu Feb 18 15:25:02 UTC 2021
On 2/18/2021 7:52 AM, Tagir Valeev wrote:
> Hello!
>
> Thank you!
>
> There's a sample in Nested Record and Array Patterns draft:
>
> static void printColorOfUpperLeftPoint(Object o) {
> if (o instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)){
> if (ul instanceof ColoredPoint(Point p, Color c)) {
> System.out.println(c);
> }
> }
> }
>
> It looks like here, the nested pattern "ul instanceof
> ColoredPoint(Point p, Color c)" is total. I thought that there's idea
> to prohibit total patterns with instanceof (at least total type test
> patterns are already prohibited). Please correct me if I'm wrong.
>
Slightly wrong :)
We prohibit total _type_ patterns in instanceof. (Alternately, we could
phrase it: we prohibit total patterns with empty remainder.) But a total
record pattern is OK, because it has remainder { null }.
Recall the motivation: avoid confusion with `o instanceof Object`. Only
the patterns where such a confusion could occur need to be restricted.
More information about the amber-spec-experts
mailing list