On 7 Jan 2021, at 6:18, Brian Goetz wrote:

…Varargs patterns will build on it (as shown in the mail); if and when Java ever gets collection literals, there will be corresponding collection patterns too.  I think the path to streamlining this is not to try and simplify the syntax of the primitive, but move upwards to higher-level patterns.

OTOH if patterns (like `switch ((O)x) case P v:` or `let P v = (O)x`) are the duals of assignment (like `x = v` or `O x = v`), then we are within our moral rights to make a pattern dualization of the venerable Java syntax `T[] x = {a,b,c}`, which is sugar for `T[] x = new T[]{a,b,c}`. The sugar allows you to take the second `T[]` (and the `new`) for a typeful context (`T[] x`).

So without the sugar we get something like:

T[] a = …;
switch (a) { case new T[]{a,b,c}: }

(The `new` from `new T[]{a,b,c}` is dropped because `new` doesn’t appear in patterns.)

But with the same sugar, but dualized, we get:

T[] a = …;
switch (a) { case {a,b,c}: }

In other words, when the pattern target is already an array, there is no need for the ceremony of repeating the array type, as with normal array declarations.

Likewise:

T[][] a2d = …;
switch (a2d) { case {{a,b},{c,d}}: }

I think this is what Tagir expected, and I think it is a reasonable “penciling out” of the basic moves of the game we are playing here.

Moving on to varargs, the context of a method call marked varargs allows elision not only of the `new T[]` in `new T[]{a,b,c}` but also the braces, you you can equally say `f(a,b,c)` or `f(new T[]{a,b,c})`.

(But not `f({a,b,c})`. So, we don’t get `f2d({{a,b},{c,d}})` by analogy with nested array initializers. Whatever.)

If a pattern-method can take a pattern-flavored argument, and perhaps a varargs argument to boot, it’s pretty clear that additional moves could follow quickly:

pattern f(pattern T[] a} { … }
pattern f2d(pattern T[][] a) { … }
pattern fv(pattern T a…) { … }
// extra “pattern” keyword on parameters for emphasis…

switch (x) {
  case f({a,b,c}): …   // omit `new T[]` b/c type
  case f2d({{d,e,f},{g}}): …   // omit `new T[][]` b/c type
  case fv(h,i,j,k): …   // omit `new T[]` and braces b/c varargs
}

Reply via email to