You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Curtis Mackie (Jira)" <ji...@apache.org> on 2021/05/12 18:55:00 UTC
[jira] [Updated] (GROOVY-10085) Class.isCase() documentation does
not clarify that Class.isCase(SomeClass) is false, or why
[ https://issues.apache.org/jira/browse/GROOVY-10085?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Curtis Mackie updated GROOVY-10085:
-----------------------------------
Summary: Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why (was: Class.isCase() documentation does not clarify that Class.isCase(SomeClass)
> Class.isCase() documentation does not clarify that Class.isCase(SomeClass) is false, or why
> -------------------------------------------------------------------------------------------
>
> Key: GROOVY-10085
> URL: https://issues.apache.org/jira/browse/GROOVY-10085
> Project: Groovy
> Issue Type: Bug
> Components: Documentation, groovy-runtime
> Affects Versions: 3.0.8, 4.0.0-alpha-3
> Environment: Pop!_OS 20.04 LTS
> Linux Kernel 5.11.0-7614-generic
> Reporter: Curtis Mackie
> Priority: Minor
>
> 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)