groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesper Steen Møller <jes...@selskabet.org>
Subject [GEP] Switch expressions syntax from Java 11 or 12 (perhaps)
Date Thu, 01 Mar 2018 15:39:42 GMT
Hi list

Java 11 (or perhaps 12) might see a new functionality known as switch expressions (https://bugs.openjdk.java.net/browse/JDK-8192963
<https://bugs.openjdk.java.net/browse/JDK-8192963>).

While the current Groovy implicit return functionality works with the switch statement as-is,
the switch expression is a more general construct, basically it is to the conditional operator
(a ? b : c) what the switch statement is to if/then/else-if/else. An example:

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> 8;
    case WEDNESDAY -> 9;
};
with 
case LABEL -> expression;
essentially sugar for
case LABEL: break expression;
As I see it: It could add utility to the Groovy language, and adopting it would keep up the
the Java-compatibility gap, which I think is a valuable gateway-drug to discovering the joys
of Groovy. The "break <expression> syntax isn't pretty, but the arrows look fine and
incur no syntax compatibility problem, as far as I can see.

Now, this being Groovy, the cases should surely support the extended "isCase"-support, as
described so well here: http://mrhaki.blogspot.dk/2009/08/groovy-goodness-switch-statement.html
<http://mrhaki.blogspot.dk/2009/08/groovy-goodness-switch-statement.html>

So, three questions remain:
1) Useful or not?
2) This Java compatibility - is it still a thing? I remember a similar proposal a little while
back, but this would align better with Java.
3) This could be implemented using existing AST's if we really want to, but it would be clumsy.
This AST transformer compatibility - is it still a thing? 

-Jesper




Mime
View raw message