Skip to content

[WiP] Switchexpression#138

Open
glelouet wants to merge 3 commits intophax:masterfrom
glelouet:switchexpression
Open

[WiP] Switchexpression#138
glelouet wants to merge 3 commits intophax:masterfrom
glelouet:switchexpression

Conversation

@glelouet
Copy link
Copy Markdown
Contributor

@glelouet glelouet commented May 9, 2026

change : java21 required
Based on master
Implementation of #136

Remaining:

  • fix java version. Can't work on 17
  • add more tests.

Done:

  • add Yield keyword
  • add SwitchExpression structure and package
  • Switchexpression can have null, default, static, or pattern caes.
  • all cases use the yield syntaxic sugar to add to the underlying block. Same for throw.
  • null is added at the end, then default. If they are equal, they should be merged (not working since expression .equals is based on ref)
  • this is required because switch expressions allow case null, default -> ; so switchexpression allows to add to both null and default, sw._null().andDefault()._throws(…)
  • static cases are the usual switch ones case 1, 2 ->
  • pattern cases allow a typed var case Integer i and therefore add var-specific addOn, yieldOn, throwOn to create the expression based on the matched variable i+1 or throw new Exception ("received"+i)
  • pattern also allows guarding when
  • all cases use the yield syntaxic sugar to add to the underlying block.
  • when printing (=state) the block, if only contains one yield then its internal expression is used instead

generator example :
https://github.com/glelouet/jcodemodel/blob/switchexpression/jcodemodeltests/src/main/java/com/helger/jcodemodel/tests/switchexpression/SwitchExpressionTestGen.java

result :
https://github.com/glelouet/jcodemodel/blob/switchexpression/jcodemodeltests/src/generated/javatest/com/helger/jcodemodel/tests/switchexpression/BasicSwitch.java

@glelouet
Copy link
Copy Markdown
Contributor Author

glelouet commented May 9, 2026

todo :

  1. java21 could not be required. if java.version<21 , then the PatternCases should be exported as if(o instance of Type name){}, all encapsulated in a callable block and yield should return instead ; then the rest of the witch should be converted to an old one (with null test if null case selected). A lot of work though.

  2. more tests , especially enums, both referenced and jdefined

@glelouet glelouet changed the title Switchexpression [WiP] Switchexpression May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant