JVM Knowledge

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 applies to case labels:

  • A pattern variable is introduced by a case label with a case pattern p if p contains a declaration of the pattern variable.
  • If a case pattern in a guarded case label contains a declaration of a pattern variable then the pattern variable is definitely matched in the associated guard.
  • A pattern variable is introduced by a guarded case label if it is introduced by the associated guard when true (§6.3.1).