Curtis Mackie created GROOVY-10085:
--------------------------------------

             Summary: Class.isCase() documentation does not clarify that 
Class.isCase(SomeClass
                 Key: GROOVY-10085
                 URL: https://issues.apache.org/jira/browse/GROOVY-10085
             Project: Groovy
          Issue Type: Bug
          Components: Documentation, groovy-runtime
    Affects Versions: 4.0.0-alpha-3, 3.0.8
         Environment: Pop!_OS 20.04 LTS
Linux Kernel 5.11.0-7614-generic
            Reporter: Curtis Mackie


>From the documentation for Groovy's java.lang.Class extensions:
{panel:title=public boolean isCase(Object switchValue)}
Special 'Case' implementation for Class, which allows testing for a certain 
class in a switch statement. For example:

 
{code:java}
switch( obj ) {
  case List :
    // obj is a list
    break;
  case Set :
    // etc
}{code}
 
{panel}
Reading that, a person might reasonably be confused by the following behavior:
{code:groovy}
class SomeClass {}

switch (SomeClass) {
    case Class:
        // we expect to go here
        println("case Class")
        break
    /* ... more cases here, presumably ... */
    default:
        // but we end up here?
        println("default")
        break
}
{code}
Looking at the code, I believe this is intentional - because TestClass is 
itself a Class, isCase() uses isAssignableFrom() rather than the expected 
isInstance(), and Class is not assignable from the unrelated SomeClass type. 
Since this case is ambiguous, the code clearly cannot do both things, and we 
shouldn't change the existing behavior in case any applications depend on it. 
However, we could provide better documentation so it's clear that using this 
with a Class will not work as expected.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to