You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "ocs@ocs.cz" <oc...@ocs.cz> on 2024/07/01 15:12:33 UTC
Re: switch does not recognise Class?
Paul,
thanks again!
Possibly (I am not entirely sure, but what do you think?) it might be better if the switch explanation was somewhat re-formulated, something like
“ Switch supports — among others — the following kinds of comparisons (see please [the isCase operator](link) for a full list):”
?
All the best,
OC
> On 30. 6. 2024, at 23:43, Paul King <pa...@asert.com.au> wrote:
>
> The isCase operator is documented under operator overloading:
> https://docs.groovy-lang.org/latest/html/documentation/#_operator_overloading
>
> With further clarification here:
> https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Class.html#isCase(java.lang.Object)
>
> But yeah, it would be better if the first bullet point in the
> switch_case explanations was clarified too.
>
> Paul.
>
> <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 7:15 AM OCsite <oc...@ocs.cz> wrote:
>>
>> Paul,
>>
>> On 30. 6. 2024, at 23:03, Paul King <pa...@asert.com.au> wrote:
>> For instances, instanceof is applied. For classes, isAssignableFrom is applied.
>>
>>
>> I see, thanks!
>>
>> Is that documented somewhere? Perhaps I'm just blind, but I can't see it here.
>>
>> All the best,
>> OC
>>
>> 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 <oc...@ocs.cz> wrote:
>>
>>
>> Christopher,
>>
>> On 30. 6. 2024, at 22:42, Christopher Smith <ch...@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 ocs@ocs.cz <oc...@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
>>
>>
>>
Re: switch does not recognise Class?
Posted by Paul King <pa...@asert.com.au>.
Sounds good. Let me know if you want to do a PR, or otherwise I'll
make the change.
Cheers, Paul.
<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 Tue, Jul 2, 2024 at 1:12 AM ocs@ocs.cz <oc...@ocs.cz> wrote:
>
> Paul,
>
> thanks again!
>
> Possibly (I am not entirely sure, but what do you think?) it might be better if the switch explanation was somewhat re-formulated, something like
>
> “ Switch supports — among others — the following kinds of comparisons (see please [the isCase operator](link) for a full list):”
>
> ?
>
> All the best,
> OC
>
> On 30. 6. 2024, at 23:43, Paul King <pa...@asert.com.au> wrote:
>
> The isCase operator is documented under operator overloading:
> https://docs.groovy-lang.org/latest/html/documentation/#_operator_overloading
>
> With further clarification here:
> https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Class.html#isCase(java.lang.Object)
>
> But yeah, it would be better if the first bullet point in the
> switch_case explanations was clarified too.
>
> Paul.
>
> <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 7:15 AM OCsite <oc...@ocs.cz> wrote:
>
>
> Paul,
>
> On 30. 6. 2024, at 23:03, Paul King <pa...@asert.com.au> wrote:
> For instances, instanceof is applied. For classes, isAssignableFrom is applied.
>
>
> I see, thanks!
>
> Is that documented somewhere? Perhaps I'm just blind, but I can't see it here.
>
> All the best,
> OC
>
> 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 <oc...@ocs.cz> wrote:
>
>
> Christopher,
>
> On 30. 6. 2024, at 22:42, Christopher Smith <ch...@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 ocs@ocs.cz <oc...@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
>
>
>
>