You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/05/12 23:30:19 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-10085: Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 4c826cf  GROOVY-10085: Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why
4c826cf is described below

commit 4c826cf7a4bb1bf18b5005ce46c1e17819c32451
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu May 13 09:04:23 2021 +1000

    GROOVY-10085: Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 45 +++++++++++++++++-----
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 9f09699..523d2bd 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -1140,15 +1140,42 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
 
     /**
      * Special 'Case' implementation for Class, which allows testing
-     * for a certain class in a switch statement.
-     * For example:
-     * <pre>switch( obj ) {
-     *   case List :
-     *     // obj is a list
-     *     break;
-     *   case Set :
-     *     // etc
-     * }</pre>
+     * whether some switch value is assignable from the given case class.
+     *
+     * If the switch value is an obj, {@code isCase} will return true if the
+     * switch value is assignment compatible with the class (case value),
+     * i.e.&nbsp;is an {@code instanceof} the class, for example:
+     * <pre class="groovyTestCase">
+     * def someList = []
+     * switch (someList) {
+     *   case List:
+     *     assert true, 'is a list'
+     *     break
+     *   case Map:
+     *     assert false, 'is not a Map'
+     *     break
+     *   default:
+     *     assert false, 'should never get here'
+     *     break
+     * }
+     * </pre>
+     *
+     * If the switch value is a class, {@code isCase} will return true if the
+     * switch value is assignable from the given class (case value), i.e.&nbsp;the case class
+     * is the same as, or a superclass, or a super-interface of the switch class, for example:
+     * <pre class="groovyTestCase">
+     * switch (ArrayList) {
+     *   case List:
+     *     assert true, 'is a list'
+     *     break
+     *   case Map:
+     *     assert false, 'is not a Map'
+     *     break
+     *   default:
+     *     assert false, 'should never get here'
+     *     break
+     * }
+     * </pre>
      *
      * @param caseValue   the case value
      * @param switchValue the switch value