You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/24 09:18:59 UTC

[groovy] branch master updated: GROOVY-9272: Switch expression (refine additional documentation)

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 8424d3a  GROOVY-9272: Switch expression (refine additional documentation)
8424d3a is described below

commit 8424d3a1258059587f955ccd5f771a4aa2ca5db2
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jul 24 17:18:43 2021 +0800

    GROOVY-9272: Switch expression (refine additional documentation)
---
 src/spec/doc/core-semantics.adoc   |  9 +++++++--
 src/spec/test/SemanticsTest.groovy | 40 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/spec/doc/core-semantics.adoc b/src/spec/doc/core-semantics.adoc
index 697c2b6..afbe6ef 100644
--- a/src/spec/doc/core-semantics.adoc
+++ b/src/spec/doc/core-semantics.adoc
@@ -189,11 +189,16 @@ Switch supports the following kinds of comparisons:
 
 NOTE: When using a closure case value, the default `it` parameter is actually the switch value (in our example, variable `x`).
 
-Groovy also supports switch expressions as shown in the following example:
+Groovy also supports switch expressions as shown in the following examples:
 
 [source,groovy]
 ----
-include::../test/SemanticsTest.groovy[tags=switch_expression,indent=0]
+include::../test/SemanticsTest.groovy[tags=switch_expression_01,indent=0]
+----
+
+[source,groovy]
+----
+include::../test/SemanticsTest.groovy[tags=switch_expression_02,indent=0]
 ----
 
 ==== Looping structures
diff --git a/src/spec/test/SemanticsTest.groovy b/src/spec/test/SemanticsTest.groovy
index e991913..863bddd 100644
--- a/src/spec/test/SemanticsTest.groovy
+++ b/src/spec/test/SemanticsTest.groovy
@@ -162,15 +162,51 @@ class SemanticsTest extends CompilableTestSupport {
 
     void testSwitchExpression() {
         def person = 'Romeo'
-        // tag::switch_expression[]
+        // tag::switch_expression_01[]
         def partner = switch(person) {
             case 'Romeo'  -> 'Juliet'
             case 'Adam'   -> 'Eve'
             case 'Antony' -> 'Cleopatra'
             case 'Bonnie' -> 'Clyde'
         }
-        // end::switch_expression[]
+        // end::switch_expression_01[]
         assert partner == 'Juliet'
+
+
+        assertScript '''\
+import groovy.transform.Immutable
+
+interface Expr { }
+@Immutable class IntExpr implements Expr { int i }
+@Immutable class NegExpr implements Expr { Expr n }
+@Immutable class AddExpr implements Expr { Expr left, right }
+@Immutable class MulExpr implements Expr { Expr left, right }
+
+int eval(Expr e) {
+// tag::switch_expression_02[]
+    switch(e) {
+        case IntExpr(i) -> i
+        case NegExpr(n) -> -eval(n)
+        case AddExpr(left, right) -> eval(left) + eval(right)
+        case MulExpr(left, right) -> eval(left) * eval(right)
+        default -> throw new IllegalStateException()
+    }
+// end::switch_expression_02[]
+}
+
+@Newify(pattern=".*Expr")
+def test() {
+    def exprs = [
+        IntExpr(4),
+        NegExpr(IntExpr(4)),
+        AddExpr(IntExpr(4), MulExpr(IntExpr(3), IntExpr(2))), // 4 + (3*2)
+        MulExpr(IntExpr(4), AddExpr(IntExpr(3), IntExpr(2)))  // 4 * (3+2)
+    ]
+    assert exprs.collect { eval(it) } == [4, -4, 10, 20]
+}
+
+test()
+        '''
     }
 
     void testClassicForLoop() {