Patterns and arguments

Remi Forax forax at
Mon Jan 18 15:59:38 UTC 2021

I think we should take a look to the difference between (sub-)patterns and arguments of a pattern.

If i want to test if o is a String with an integer value greater than 10, i can write something like
  switch(o) {
    case String s && Integer.parseInt(s, >10) -> ...

but this pseudo syntax is not a good one because it's like the argument of the pattern (s) and the sub-pattern (>10) are both "arguments" of the pattern Integer.parseInt.

For the shake of the demonstration, let's use a syntax that cleanly separate the arguments from the sub-pattern,
with the arguments in between '{' '}' and the sub-pattern in between '[' ']'

  switch(o) {
    case String s && Integer.parseInt{s} [>10] -> ...

I think it's important to separate the two because they don't obey the same rules,
the arguments can be any expressions while the sub-pattern can only be a pattern.

By example,{1}[...] is valid while{...}[1] is not (with ... meaning whatever works).
Because 1 is a valid argument while 1 is not a valid pattern.

If we separate the arguments and the pattern, this has several implications
- until now, we have used parenthesis as patterns separator, but given that we has used parenthesis for arguments since Java 1.0,
  we may want to revisit the use of parenthesis for the sub-patterns
  - we can also use parenthesis for both but it may be confusing.
- we may not need 'var' because it can only appear in the sub-pattern part, may be the fact that there is a specific delimiter for that part is enough.


More information about the amber-spec-experts mailing list