Introduced by a guard? (Question on Draft Spec for JEP 440 and 441)
Gavin Bierman
gavin.bierman at oracle.com
Tue May 23 11:25:40 UTC 2023
Hi David,
Sorry for my slow reply.
There are other expressions that can introduce a pattern variable, although one could argue whether they would occur naturally, for example, B && (n instanceof Integer i), where B is some other boolean expression (and other variations with nested &&). It’s always a balance trying to keep things general, or lock them down. In this case, I’ve erred on the general side of the fence. One other reason for this is that we may in the future add some other expression forms that introduce pattern variables, and our future selves will be grateful for fewer things in the spec to change.
I’m grateful for the attention to detail though - thanks for asking!
Gavin
> On 9 Apr 2023, at 01:27, David Alayachew <davidalayachew at gmail.com> wrote:
>
> Hello Amber Dev Team,
>
> Thank you Gavin for posting the spec!
>
> > 6.3.4 Scope for Pattern Variables in Switch Labels
> >
> > Pattern variables can be introduced by case labels with a
> > case pattern, either by the pattern itself or by a guard,
> > and are in scope for the relevant parts of the associated
> > switch expression (6.3.1.6) or switch statement (6.3.2.6)
> >
> > The following rules apply to case labels:
> >
> > * A pattern variable is introduced by a case label
> > with a case pattern p if it is declared by p.
> >
> > * A pattern variable declared by the pattern of a
> > guarded case pattern is definitely matched in the
> > associated guard.
> >
> > - It is a compile-time error if any pattern
> > variable declared by the pattern of a guarded
> > case pattern is already in scope at its guard.
> >
> > * A pattern variable is introduced by a guarded case
> > label if it is introduced by the associated guard
> > when true (6.3.1).
>
> Reading the bullets here (especially the final bullet point) implies to me that the only time that the guard of a guarded case pattern can introduce variables is when the guard contains a pattern match itself. So, something like this.
>
> Number input = 4;
>
> String response =
> switch (input) {
> case Number n when n instanceof Integer i -> String.valueOf(i);
> default -> "idc";
> };
>
> Apologies for the terrible example.
>
> However, when reading the sentences in the quoted text above the bullets, they seem a lot more loose. More specifically, reading that text makes me think something like this is possible (obviously illegal and nonsensical code, but trying to make a point).
>
> switch (someObject)
> {
>
> case Integer i when (int abc = method()) == i -> "???";
> default -> "idk";
>
> };
>
> Apologies for the terrible example.
>
> I guess my question is, is there another way besides patterns for a variable to be introduced in the guard?
>
> And if not, would it make more sense to instead say something like the following instead?
>
> > Pattern variables can be introduced by case labels with a
> > case pattern, either by the pattern itself or by a guard
> > containing a pattern, and are in scope for the relevant
> > parts of the associated switch expression (6.3.1.6) or
> > switch statement (6.3.2.6)
>
> Apologies if this is just noise, but I wanted to bring it up because I initially parsed the quoted passage to imply the second code example I gave. It wasn't until several minutes of reading and screwing around in jshell did I start to think the passage was actually implying the first code example I gave.
>
> Thank you for your time and help!
> David Alayachew
More information about the amber-dev
mailing list