<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Hello Amber Dev Team,<br><br>Thank you Gavin for posting the spec!<br><br>> 6.3.4 Scope for Pattern Variables in Switch Labels<br>> <br>> Pattern variables can be introduced by case labels with a<br>> case pattern, either by the pattern itself or by a guard,<br>> and are in scope for the relevant parts of the associated<br>> switch expression (6.3.1.6) or switch statement (6.3.2.6)<br>> <br>> The following rules apply to case labels:<br>> <br>>    * A pattern variable is introduced by a case label<br>>      with a case pattern p if it is declared by p.<br>> <br>>    * A pattern variable declared by the pattern of a<br>>      guarded case pattern is definitely matched in the<br>>      associated guard.<br>> <br>>        - It is a compile-time error if any pattern<br>>          variable declared by the pattern of a guarded<br>>          case pattern is already in scope at its guard.<br>> <br>>    * A pattern variable is introduced by a guarded case<br>>      label if it is introduced by the associated guard<br>>      when true (6.3.1).<br><br>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.<br><br>    Number input = 4;<br>    <br>    String response = <br>        switch (input) {<br>            case Number n when n instanceof Integer i -> String.valueOf(i);<br>            default -> "idc";<br>        };<br><br>Apologies for the terrible example.<br><br>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).<br><br>    switch (someObject)<br>    {<br>    <br>        case Integer i when (int abc = method()) == i -> "???";<br>        default -> "idk";<br>    <br>    };<br><br>Apologies for the terrible example.<br><br>I guess my question is, is there another way besides patterns for a variable to be introduced in the guard?<br><br>And if not, would it make more sense to instead say something like the following instead?<br><br>> Pattern variables can be introduced by case labels with a<br>> case pattern, either by the pattern itself or by a guard<br>> containing a pattern, and are in scope for the relevant<br>> parts of the associated switch expression (6.3.1.6) or<br>> switch statement (6.3.2.6)<br><br>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.<br><br>Thank you for your time and help!<br>David Alayachew</div></div>