[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17427671#comment-17427671 ] Paul King commented on GROOVY-9272: --- The language docs mentions switch expressions at core-semantics.adoc#L192. Very brief but help to expand would be greatly appreciated. For the release notes, switch expressions are the first section in new features: https://groovy-lang.org/releasenotes/groovy-4.0.html#Groovy4.0-new That changelog looks incomplete. GROOVY-9272 doesn't even seem to be in the complete list of issues for all 4 releases: https://groovy-lang.org/changelogs/changelog-4.0.0-unreleased.html (I don't know whether it was temporarily marked as unresolved during the release process - history doesn't seem to say so - I'll have to investigate more before the next release.) > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Assignee: Daniel Sun >Priority: Major > Fix For: 4.0.0-beta-1 > > Time Spent: 1.5h > Remaining Estimate: 0h > > See JEP 361( [https://openjdk.java.net/jeps/361] ) > > The following references are a bit outdated: > JEP 354( [https://openjdk.java.net/jeps/354] ) > JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17423983#comment-17423983 ] Eric Milles commented on GROOVY-9272: - Note: this item did not make it into the release notes or the language docs. http://groovy-lang.org/changelogs/changelog-4.0.0-beta-1.html > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Assignee: Daniel Sun >Priority: Major > Fix For: 4.0.0-beta-1 > > Time Spent: 1.5h > Remaining Estimate: 0h > > See JEP 361( [https://openjdk.java.net/jeps/361] ) > > The following references are a bit outdated: > JEP 354( [https://openjdk.java.net/jeps/354] ) > JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17386705#comment-17386705 ] Daniel Sun commented on GROOVY-9272: FYI. groovy deconstruction has been reverted: https://github.com/apache/groovy/commit/fb502bd53023838b735a0fda3e825fad5bf8dd1e > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Assignee: Daniel Sun >Priority: Major > Fix For: 4.0.0-beta-1 > > Time Spent: 1.5h > Remaining Estimate: 0h > > See JEP 361( [https://openjdk.java.net/jeps/361] ) > > The following references are a bit outdated: > JEP 354( [https://openjdk.java.net/jeps/354] ) > JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17386683#comment-17386683 ] Daniel Sun commented on GROOVY-9272: {code:java} int eval(Expr n) { return switch(n) { case IntExpr(int i) -> i; case NegExpr(Expr n) -> -eval(n); case AddExpr(Expr left, Expr right) -> eval(left) + eval(right); case MulExpr(Expr left, Expr right) -> eval(left) * eval(right); default -> throw new IllegalStateException(); }; } {code} [~paulk] Destructuring is supported now: https://github.com/apache/groovy/commit/8424d3a1258059587f955ccd5f771a4aa2ca5db2 > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Assignee: Daniel Sun >Priority: Major > Fix For: 4.0.0-beta-1 > > Time Spent: 1.5h > Remaining Estimate: 0h > > See JEP 361( [https://openjdk.java.net/jeps/361] ) > > The following references are a bit outdated: > JEP 354( [https://openjdk.java.net/jeps/354] ) > JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17350441#comment-17350441 ] Eric Milles commented on GROOVY-9272: - Thanks for working through this. Having the parser changes in place will allow for experiments on the AST side without having duplicate effort in antlr4. I like the closure translation as a low-impact first step. > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > Time Spent: 20m > Remaining Estimate: 0h > > See JEP 354( [https://openjdk.java.net/jeps/354] ) > > The following reference is a bit outdated: > JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17349629#comment-17349629 ] Daniel Sun commented on GROOVY-9272: Here is the proposed PR: https://github.com/apache/groovy/pull/1580 > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > Time Spent: 10m > Remaining Estimate: 0h > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17348926#comment-17348926 ] Daniel Sun commented on GROOVY-9272: {quote} It sounds like there was a prototype for the ClosureExpression option. Does that still exist? That first option does still have the benefit of simplicity. It may give Groovy most of what the Java switch expression provides. {quote} Yep. We already have a prototype for the ClosureExpression option in 2019/11/5. The implementation still exists but need a little polishment, e.g. support {{yield}}, abandon support for mixed {{:}} and {{->}} > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17348005#comment-17348005 ] Paul King commented on GROOVY-9272: --- It is also worth looking at what might be supported in future Java versions: https://openjdk.java.net/jeps/405 JEP 405: Record Patterns & Array Patterns (Preview) https://openjdk.java.net/jeps/406 JEP 406: Pattern Matching for switch (Preview) A nested record pattern: {code} static void printColorOfUpperLeftPoint(Rectangle r) { if (r instanceof Rectangle(ColoredPoint(Point p, Color c), ColoredPoint lr)) { System.out.println(c); } } {code} An array pattern: {code} static void printFirstTwoStrings(Object o) { if (o instanceof String[] { String s1, String s2, ... }){ System.out.println(s1 + s2); } } {code} Record patterns and array patterns could potentially be used within switch targets. Guarded patterns in switch: {code} static void testTriangle(Shape s) { switch (s) { case null -> System.out.println("Null!"); case Triangle t && (t.calculateArea() > 100) -> System.out.println("Large triangle"); case Triangle t -> System.out.println("Small triangle"); default -> System.out.println("Non-triangle"); } } {code} Destructuring is still out of scope for JEP 406 but something we might want to consider: {code} int eval(Expr n) { return switch(n) { case IntExpr(int i) -> i; case NegExpr(Expr n) -> -eval(n); case AddExpr(Expr left, Expr right) -> eval(left) + eval(right); case MulExpr(Expr left, Expr right) -> eval(left) * eval(right); default -> throw new IllegalStateException(); }; } {code} As per our exploration of possible ideas from Pythons related proposal: https://www.python.org/dev/peps/pep-0622/ > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17346928#comment-17346928 ] Eric Milles commented on GROOVY-9272: - It sounds like there was a prototype for the {{ClosureExpression}} option. Does that still exist? That first option does still have the benefit of simplicity. It may give Groovy most of what the Java switch expression provides. > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17346922#comment-17346922 ] Eric Milles commented on GROOVY-9272: - For the ternary option, what if "c" is a non-trivial expression? You are repeating it once per comparison. The proposed options appear to be: * create {{ClosureExpression}} with {{SwitchStatement}} * create {{TernaryExpression}} chain with {{ClosureExpression}} for any case with block * create {{BlockExpression}} -- then there must be some switch or something to make this work * create {{SwitchExpression}} with semantics similar to {{SwitchStatement}} (statements for case and default); {{ReturnStatement}} interpreted as "yield" -- I suspect you will need a new expression type to handle the different scenarios that Java 16 provides. Compiler needs to error for block without {{yield}} or with {{break}}, {{continue}} or {{return}}. http://groovy.329449.n5.nabble.com/VOTE-Support-switch-expression-in-the-next-version-td5762208.html https://issues.apache.org/jira/browse/GROOVY-10090 > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17345576#comment-17345576 ] Daniel Sun commented on GROOVY-9272: If we support {{BlockExpression}} proposed in GROOVY-10090, we could simplify the transformed expression as: Note: we use {{(statement1;statement2;statementN;...)}} to represent {{BlockExpression}} {code:groovy} def result = c == 'a' ? 0 : c == 'b' ? 1 : c == 'c' ? (int len = c.length(); yield len) : (throw new IllegalArgumentException(c)) {code} > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17345005#comment-17345005 ] Daniel Sun commented on GROOVY-9272: See also: https://docs.oracle.com/en/java/javase/16/language/switch-expressions.html > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17345001#comment-17345001 ] Daniel Sun commented on GROOVY-9272: If no one has started the work, I would propose transforming the AST for "switch expression" to "ternary expression", thus we could reuse all of the work of type inference, bytecode generation, etc. For example, {code:groovy} def result = switch (c) { case 'a' -> 0 case 'b' -> 1 case 'c' -> { int len = c.length(); yield len } default -> throw new IllegalArgumentException(c) } {code} We could transform the above code as follows: {code:groovy} def result = c == 'a' ? 0 : c == 'b' ? 1 : c == 'c' ? {-> int len = c.length(); return len}() : {-> throw new IllegalArgumentException(c)}() {code} > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)
[jira] [Commented] (GROOVY-9272) Support switch expression
[ https://issues.apache.org/jira/browse/GROOVY-9272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17265380#comment-17265380 ] Paul King commented on GROOVY-9272: --- Useful summary about treatment of {{yield}} and when limited lookahead for variables called {{yield}} is needed: https://bugs.openjdk.java.net/browse/JDK-8223305 > Support switch expression > - > > Key: GROOVY-9272 > URL: https://issues.apache.org/jira/browse/GROOVY-9272 > Project: Groovy > Issue Type: New Feature >Reporter: Daniel Sun >Priority: Major > > See JEP 325( [https://openjdk.java.net/jeps/325] ) > [https://blog.codefx.org/java/switch-expressions/] > [https://dzone.com/articles/jdk-12-switch-statementsexpressions-in-action] > [https://blog.idrsolutions.com/2019/03/java-12-switch-expressions-explained-in-5-minutes/] > -- This message was sent by Atlassian Jira (v8.3.4#803005)