Array patterns (and varargs patterns)

Suminda Sirinath Salpitikorala Dharmasena sirinath1978m at gmail.com
Wed Jan 6 15:38:29 UTC 2021


Other languages have patent matching on head and tail of lists. Adding this
may mean there should be additional syntax. So for the time being simplest
is the ability to match 1st few elements and the whole array like what I
have given below if it can be supported:

       if (arr instanceof String[] { var a, var b, ... } stringArray) { ...
}

Another issue is if the array is shorter say 0 length and one has:

       if (arr instanceof int[] { var a, var b, ... }) { ... }

Will this be an ArrayIndexOutOfBoundException or another exception? If it
is a reference type `a` and `b` can be null.



On Wed, 6 Jan 2021 at 15:31, Gavin Bierman <gavin.bierman at oracle.com> wrote:

> This is a feature that other languages call an “as pattern”, because you
> are adding a new pattern variable binding to another pattern (Standard ML
> used “as”, Haskell uses “@“ but calls it an as pattern). It’s on our list
> of things to consider; part of the consideration is whether it merits
> special syntax along with the parsing issues of not having special syntax.
>
> Gavin
>
> > On 6 Jan 2021, at 05:44, Suminda Sirinath Salpitikorala Dharmasena <
> sirinath1978m at gmail.com> wrote:
> >
> > Can we have something like:
> >
> >        if (arr instanceof String[] { var a, var b, ... } stringArray) {
> > ... }
> >
> > `stringArray` is optional but if present this will referrer to the whole
> > array.
> >
> >>
>
>


More information about the amber-dev mailing list