Pattern features for next iteration

Gavin Bierman Gavin.Bierman at oracle.com
Thu Jan 21 11:27:15 UTC 2021



> On 20 Jan 2021, at 06:42, Suminda Sirinath Salpitikorala Dharmasena <sirinath1978m at gmail.com> wrote:
> 
> Can we accomodate something like:
>   if (strarr instanceof String[] {var head, var... tail} wholestrarr)
> or
>   if (strarr instanceof String[] {var head, var... tail})
> or
>   if (strarr instanceof String[] {String head, String... tail} wholestrarr)
> 
> or
>   if (strarr instanceof String[] {String head, String... tail})

I’m not sure I can figure out what these patterns might mean. The form of the array pattern is supposed to match up with the array creation expressions, so we get a nice symmetry

new String[] { “one”, “two” } matches String[] { var a, var b}
and
new String[] { “one”, “two”, “three" } matches String[] { var a, var b, … }

Sure we could go crazy and start offering binding for portions of the array, but that would imply that pattern matching will be in the business of array creation, which I think would be a very confusing model for developers. 


> 
> Also need to consider situations like:
>   if (true || (strarr instanceof String[] wholestrarr)) {
>      // what is wholestrarr here? Is it null? Is it a ClassCastException?

It is not in scope “here”. This is dealt with in the current JEP 394. 

> }
> is this similar to
>   if (true || (strarr instanceof String[])) {
>      String[] wholestrarr = (String[]) strarr;
>   }
> 
> What about:
>   if ((arr instanceof int[] intarr) || (arr instanceof String[] strarr)) {
>      // what is intarr / strarr here? Is one of them null? Is it a
> ClassCastException?
>   }


Gavin


More information about the amber-spec-observers mailing list