For instances, instanceof is applied. For classes, isAssignableFrom is applied.

You can always check by looking at isCase.

assert String.isCase('foo')
assert !Class.isCase(String)
assert CharSequence.isCase(String)
assert Object.isCase(Class)

assert switch(String) {
    case Class -> false
    case CharSequence -> true
}

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Mon, Jul 1, 2024 at 6:52 AM OCsite <o...@ocs.cz> wrote:
>
> Christopher,
>
> On 30. 6. 2024, at 22:42, Christopher Smith <chry...@gmail.com> wrote:
>
> You're comparing `class java.lang.String` and `class java.lang.Class`.
>
> No, I'm not.
>
> Which rule in the docs leads you to expect this to be truthy?
>
> The very first documented one, namely
>
> Class case values match if the switch value is an instance of the class
>
> Each class is an instance of java.lang.Class (as actually proves the second 
> case which checks it explicitly through a closure; since it is there, I 
> thought there's no need to elaborate).
>
> Thanks and all the best,
> OC
>
>
> On Sun, Jun 30, 2024, 13:17 o...@ocs.cz <o...@ocs.cz> wrote:
>>
>> Hi there,
>>
>> is this the intended behaviour?
>>
>> ===
>> groovy:000> switch (String) { case Class: println "C"; break; case {it 
>> instanceof Class}: println "CC" }
>> CC
>> ===> null
>> groovy:000>
>> ===
>>
>> Based on the switch documented semantic I would presume "C" should be 
>> printed out, not "CC"?
>>
>> Thanks and all the best,
>> OC
>>
>

Reply via email to