<div dir="ltr">Hello.<br><br>I would like to improve my understanding of the interaction of two JEPs: Pattern matching for switch and Record patterns.<br><br>I am new to this list so might have missed previous discussion about this but could not find what I was looking for in the archives.<br><br>JEP 441 shows the following grammar for switch labels.<br><br>SwitchLabel:<br>  case CaseConstant { , CaseConstant }<br>  case null [, default]<br>  case Pattern [ Guard ]<br>  default<br><br>JEP 440 shows the following grammar for patterns.<br><br>Pattern:<br>  TypePattern<br>  RecordPattern<br><br>TypePattern:<br>  LocalVariableDeclaration<br><br>RecordPattern:<br>  ReferenceType ( [ PatternList ] )<br><br>PatternList : <br>  Pattern { , Pattern }<br><br>As a consequence it is not possible to have case constants in the pattern list of a record pattern. For example consider the following definitions modeling (a simplified version of) boolean expressions.<br><br>sealed interface BoolExpr {<br>    enum Constant implements BoolExpr { TRUE, FALSE }<br>    record And(BoolExpr left, BoolExpr right) implements BoolExpr {}<br><br>    default BoolExpr recursively(UnaryOperator<BoolExpr> transform) {<br>        return transform.apply(switch (this) {<br>            case Constant c -> c;<br>            case And(var left, var right) -> new And(<br>
                left.recursively(transform),<br>
                right.recursively(transform)<br>
            

);<br>        });<br><div>    }</div><div><br></div>    default BoolExpr shortCircuit() {<br>        return recursively(expr -> switch (expr) {<br>            case And(var left, var unused)<br>
                when left == Constant.FALSE -> Constant.FALSE;<br>            default -><br>                expr;<br>        });<br>    }<br>}<br><br>In the definition of `shortCircuit` (which does partial evaluation) we need to use a guard to check if the left argument of `And` is false.<br><br>Instead of using an enum, we can model constants as empty records as follows.<br><br>    sealed interface Constant extends BoolExpr permits True, False {}<br>    record True() implements Constant {}<br>    record False() implements Constant {}<br><br>Now we can write `shortCircuit` using a nested record pattern.<br><br>    default BoolExpr shortCircuit() {<br>        return recursively(expr -> switch (expr) {<br>            case And(False(), var unused) -> new False();<br>            default -> expr;<br>        });<br>    }<br><br>It would be convenient to be able to use a nested pattern instead of a guard with the original definition using an enum for constants. Here is a hypothetical implementation of `shortCircuit` that is currently not supported.<br><br>    default BoolExpr shortCircuit() {<br>        return recursively(expr -> switch (expr) {<br>            case And(Constant.FALSE, var unused) -> Constant.FALSE;<br>            default -> expr;<br>        });<br>    }<br><br>What are potential problems with allowing case constants as nested patterns?<br><br>Kind regards,<br>Sebastian<br><br><br><br><br><br></div>