Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
3.0.8, 4.0.0-alpha-3
-
None
-
Pop!_OS 20.04 LTS
Linux Kernel 5.11.0-7614-generic
Description
From the documentation for Groovy's java.lang.Class extensions:
Special 'Case' implementation for Class, which allows testing for a certain class in a switch statement. For example:
switch( obj ) { case List : // obj is a list break; case Set : // etc }
Reading that, a person might reasonably be confused by the following behavior:
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 }
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.