Hi all, Though Java's switch expression is still in preview phase, Groovy has followed up its steps in my labs project:
https://github.com/danielsun1106/groovy-parser/tree/switch-expression As you can see, switch expressions can be parsed and executed. For some edge cases, We may polish the implementation further. The rationale is to transform switch expression to switch statement in closure, e.g. The original code: ``` def a = 6 def r = switch (a) { case 6, 8 -> 'x' case 9 -> 'y' default -> 'z' } assert 'x' == r ``` The transformed code: ``` def a = 6 def r = {-> def result = null switch (a) { case 6: case 8: result = 'x'; break; case 9: result = 'y'; break; default: result = 'z'; break; } return result }() assert 'x' == r ``` Groovy always provides extra friendly features, which cover switch expression absolutely ;-) For example: ``` def a = 6 def r = switch (a) { case 6, 8 : { // in Java, error occurs as no `yield` found. // But in Groovy, you can think it as an optional `yield`, which is a bit similar to optional 'return' in methods 'x' } case 9 -> 'y' // `case ... :`(colon) and `case ... ->`(arrow) can be mixed as you need default -> 'z' } assert 'x' == r ``` Please vote on supporting switch expression in the next version, e.g. Groovy 3.1.0 The vote is open for the next 72 hours and passes if a majority of at least three +1 PMC votes are cast. [ ] +1 Support switch expression in the next version [ ] 0 I don't have a strong opinion about this, but I assume it's ok [ ] -1 Do not support switch expression because... Here is my vote: +1 (binding) Cheers, Daniel.Sun ----- Apache Groovy committer & PMC member Blog: http://blog.sunlan.me Twitter: @daniel_sun -- Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html