You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Ferenc Nagy <nf...@gmail.com> on 2023/08/15 03:19:00 UTC

Elvis operator and type casting precedence in Groovy

Hi Devs,

I've posted a question about the topic here:
https://stackoverflow.com/questions/76863969/elvis-operator-and-type-casting-precedence-in-groovy/76900266#76900266

Just repeating the same here and I'm curious about your opinion. Should I
open a bug ticket for this as suggested in the answer in SOF?

---

Let's take the following simple expression:

((Double) null ?: 0).getClass()

Results:

   - Groovy 3: class java.lang.Double
   - Groovy 4: class java.lang.Integer

Does anyone know the reason for the different behaviour? I'd say Groovy 4
is correct since the casting is applied before the Elvis operator.

Checked, but couldn't find anything related in the Groovy 4 release notes:
https://groovy-lang.org/releasenotes/groovy-4.0.html


---


Regards,

Ferenc

Re: Elvis operator and type casting precedence in Groovy

Posted by Ferenc Nagy <nf...@gmail.com>.
Hi Jim and Paul,

Mystery has been solved :)
Thanks for your help and investigation, much appreciated.

Regards,
Ferenc

Paul King <pa...@asert.com.au> ezt írta (időpont: 2023. aug. 15., K, 19:25):

> It looks like it was this issue/commit:
>
> https://issues.apache.org/jira/browse/GROOVY-8965
> https://github.com/apache/groovy/commit/d94ea203ea
>
> So Jim's hunch was right. A fix for a specific issue, fixed a broader set
> of things. Fragments of the behavior are covered in various STC tests but
> it does look like we need better test coverage for the dynamic cases.
>
> 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>
> <#m_1460524499199556130_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Tue, Aug 15, 2023 at 3:36 PM Paul King <pa...@asert.com.au> wrote:
>
>> Hi Jim, I hope you are well, long time no see, and also hi Ferenc, thanks
>> for raising the issue.
>>
>> I haven't tracked down the actual commit yet but the current behavior
>> seems correct to me too. It changed in 4.0.5. The release notes for
>> 4.0.5[1] also don't mention elvis but do mention ternary operator - not the
>> same thing but share some code paths - though the issues don't look
>> particularly likely. There are also two issues mentioning casts but again
>> not likely candidates at face value. The CST for both 4.0.4 and 4.0.5 are
>> the same, so it's not a grammar thing. Git bisect would probably be the
>> next step in tracking down the change.
>>
>> [1] https://issues.apache.org/jira/projects/GROOVY/versions/12352126
>>
>>
>>
>> <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>
>> <#m_1460524499199556130_m_-3670125639948008350_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>
>> On Tue, Aug 15, 2023 at 2:06 PM Jim White <ja...@gmail.com>
>> wrote:
>>
>>> Hi Ferenc,
>>>
>>> I don't see anything about that in the release notes either and I tried
>>> this search in JIRA (text=elvis && status=unresolved | fixed
>>> <https://issues.apache.org/jira/browse/GROOVY-11014?jql=project%20%3D%20GROOVY%20AND%20resolution%20in%20(Unresolved%2C%20Fixed)%20AND%20text%20~%20%22elvis%22%20ORDER%20BY%20cf%5B12312521%5D%20DESC%2C%20priority%20DESC%2C%20updated%20DESC>)
>>> which doesn't appear to have anything in the v3 vs v4 timeframe that looks
>>> related.  It is quite plausible that the change involved wasn't specific to
>>> the elvis operator and is described for some more general change to the
>>> type system.  There is a recent open issue that talks about something
>>> similar so maybe something has changed not too long ago:
>>> https://issues.apache.org/jira/browse/GROOVY-11014
>>>
>>> In any case I agree with the SO answer that the v4 behavior is correct
>>> (i.e. the type of the elvis condition value should only be relevant when it
>>> is truthy) but I'm not up to date on all the various
>>> compiler type inference things in Groovy these days.
>>>
>>> Notably there don't appear to be any type inference related tests for
>>> the elvis operator:
>>>
>>> https://github.com/apache/groovy/blob/95f36bc52cdca47ca7722e45aabb57152dcc86f7/src/test/groovy/operator/TernaryOperatorsTest.groovy#L55
>>>
>>> Jim
>>>
>>>
>>>
>>>
>>> On Mon, Aug 14, 2023 at 8:19 PM Ferenc Nagy <nf...@gmail.com> wrote:
>>>
>>>> Hi Devs,
>>>>
>>>> I've posted a question about the topic here:
>>>>
>>>> https://stackoverflow.com/questions/76863969/elvis-operator-and-type-casting-precedence-in-groovy/76900266#76900266
>>>>
>>>> Just repeating the same here and I'm curious about your opinion. Should
>>>> I open a bug ticket for this as suggested in the answer in SOF?
>>>>
>>>> ---
>>>>
>>>> Let's take the following simple expression:
>>>>
>>>> ((Double) null ?: 0).getClass()
>>>>
>>>> Results:
>>>>
>>>>    - Groovy 3: class java.lang.Double
>>>>    - Groovy 4: class java.lang.Integer
>>>>
>>>> Does anyone know the reason for the different behaviour? I'd say Groovy
>>>> 4 is correct since the casting is applied before the Elvis operator.
>>>>
>>>> Checked, but couldn't find anything related in the Groovy 4 release
>>>> notes: https://groovy-lang.org/releasenotes/groovy-4.0.html
>>>>
>>>>
>>>> ---
>>>>
>>>>
>>>> Regards,
>>>>
>>>> Ferenc
>>>>
>>>

Re: Elvis operator and type casting precedence in Groovy

Posted by Paul King <pa...@asert.com.au>.
It looks like it was this issue/commit:

https://issues.apache.org/jira/browse/GROOVY-8965
https://github.com/apache/groovy/commit/d94ea203ea

So Jim's hunch was right. A fix for a specific issue, fixed a broader set
of things. Fragments of the behavior are covered in various STC tests but
it does look like we need better test coverage for the dynamic cases.

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, Aug 15, 2023 at 3:36 PM Paul King <pa...@asert.com.au> wrote:

> Hi Jim, I hope you are well, long time no see, and also hi Ferenc, thanks
> for raising the issue.
>
> I haven't tracked down the actual commit yet but the current behavior
> seems correct to me too. It changed in 4.0.5. The release notes for
> 4.0.5[1] also don't mention elvis but do mention ternary operator - not the
> same thing but share some code paths - though the issues don't look
> particularly likely. There are also two issues mentioning casts but again
> not likely candidates at face value. The CST for both 4.0.4 and 4.0.5 are
> the same, so it's not a grammar thing. Git bisect would probably be the
> next step in tracking down the change.
>
> [1] https://issues.apache.org/jira/projects/GROOVY/versions/12352126
>
>
>
> <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>
> <#m_-3670125639948008350_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Tue, Aug 15, 2023 at 2:06 PM Jim White <ja...@gmail.com>
> wrote:
>
>> Hi Ferenc,
>>
>> I don't see anything about that in the release notes either and I tried
>> this search in JIRA (text=elvis && status=unresolved | fixed
>> <https://issues.apache.org/jira/browse/GROOVY-11014?jql=project%20%3D%20GROOVY%20AND%20resolution%20in%20(Unresolved%2C%20Fixed)%20AND%20text%20~%20%22elvis%22%20ORDER%20BY%20cf%5B12312521%5D%20DESC%2C%20priority%20DESC%2C%20updated%20DESC>)
>> which doesn't appear to have anything in the v3 vs v4 timeframe that looks
>> related.  It is quite plausible that the change involved wasn't specific to
>> the elvis operator and is described for some more general change to the
>> type system.  There is a recent open issue that talks about something
>> similar so maybe something has changed not too long ago:
>> https://issues.apache.org/jira/browse/GROOVY-11014
>>
>> In any case I agree with the SO answer that the v4 behavior is correct
>> (i.e. the type of the elvis condition value should only be relevant when it
>> is truthy) but I'm not up to date on all the various
>> compiler type inference things in Groovy these days.
>>
>> Notably there don't appear to be any type inference related tests for the
>> elvis operator:
>>
>> https://github.com/apache/groovy/blob/95f36bc52cdca47ca7722e45aabb57152dcc86f7/src/test/groovy/operator/TernaryOperatorsTest.groovy#L55
>>
>> Jim
>>
>>
>>
>>
>> On Mon, Aug 14, 2023 at 8:19 PM Ferenc Nagy <nf...@gmail.com> wrote:
>>
>>> Hi Devs,
>>>
>>> I've posted a question about the topic here:
>>>
>>> https://stackoverflow.com/questions/76863969/elvis-operator-and-type-casting-precedence-in-groovy/76900266#76900266
>>>
>>> Just repeating the same here and I'm curious about your opinion. Should
>>> I open a bug ticket for this as suggested in the answer in SOF?
>>>
>>> ---
>>>
>>> Let's take the following simple expression:
>>>
>>> ((Double) null ?: 0).getClass()
>>>
>>> Results:
>>>
>>>    - Groovy 3: class java.lang.Double
>>>    - Groovy 4: class java.lang.Integer
>>>
>>> Does anyone know the reason for the different behaviour? I'd say Groovy
>>> 4 is correct since the casting is applied before the Elvis operator.
>>>
>>> Checked, but couldn't find anything related in the Groovy 4 release
>>> notes: https://groovy-lang.org/releasenotes/groovy-4.0.html
>>>
>>>
>>> ---
>>>
>>>
>>> Regards,
>>>
>>> Ferenc
>>>
>>

Re: Elvis operator and type casting precedence in Groovy

Posted by Paul King <pa...@asert.com.au>.
Hi Jim, I hope you are well, long time no see, and also hi Ferenc, thanks
for raising the issue.

I haven't tracked down the actual commit yet but the current behavior seems
correct to me too. It changed in 4.0.5. The release notes for 4.0.5[1] also
don't mention elvis but do mention ternary operator - not the same thing
but share some code paths - though the issues don't look particularly
likely. There are also two issues mentioning casts but again not likely
candidates at face value. The CST for both 4.0.4 and 4.0.5 are the same, so
it's not a grammar thing. Git bisect would probably be the next step in
tracking down the change.

[1] https://issues.apache.org/jira/projects/GROOVY/versions/12352126


<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, Aug 15, 2023 at 2:06 PM Jim White <ja...@gmail.com>
wrote:

> Hi Ferenc,
>
> I don't see anything about that in the release notes either and I tried
> this search in JIRA (text=elvis && status=unresolved | fixed
> <https://issues.apache.org/jira/browse/GROOVY-11014?jql=project%20%3D%20GROOVY%20AND%20resolution%20in%20(Unresolved%2C%20Fixed)%20AND%20text%20~%20%22elvis%22%20ORDER%20BY%20cf%5B12312521%5D%20DESC%2C%20priority%20DESC%2C%20updated%20DESC>)
> which doesn't appear to have anything in the v3 vs v4 timeframe that looks
> related.  It is quite plausible that the change involved wasn't specific to
> the elvis operator and is described for some more general change to the
> type system.  There is a recent open issue that talks about something
> similar so maybe something has changed not too long ago:
> https://issues.apache.org/jira/browse/GROOVY-11014
>
> In any case I agree with the SO answer that the v4 behavior is correct
> (i.e. the type of the elvis condition value should only be relevant when it
> is truthy) but I'm not up to date on all the various
> compiler type inference things in Groovy these days.
>
> Notably there don't appear to be any type inference related tests for the
> elvis operator:
>
> https://github.com/apache/groovy/blob/95f36bc52cdca47ca7722e45aabb57152dcc86f7/src/test/groovy/operator/TernaryOperatorsTest.groovy#L55
>
> Jim
>
>
>
>
> On Mon, Aug 14, 2023 at 8:19 PM Ferenc Nagy <nf...@gmail.com> wrote:
>
>> Hi Devs,
>>
>> I've posted a question about the topic here:
>>
>> https://stackoverflow.com/questions/76863969/elvis-operator-and-type-casting-precedence-in-groovy/76900266#76900266
>>
>> Just repeating the same here and I'm curious about your opinion. Should I
>> open a bug ticket for this as suggested in the answer in SOF?
>>
>> ---
>>
>> Let's take the following simple expression:
>>
>> ((Double) null ?: 0).getClass()
>>
>> Results:
>>
>>    - Groovy 3: class java.lang.Double
>>    - Groovy 4: class java.lang.Integer
>>
>> Does anyone know the reason for the different behaviour? I'd say Groovy 4
>> is correct since the casting is applied before the Elvis operator.
>>
>> Checked, but couldn't find anything related in the Groovy 4 release
>> notes: https://groovy-lang.org/releasenotes/groovy-4.0.html
>>
>>
>> ---
>>
>>
>> Regards,
>>
>> Ferenc
>>
>

Re: Elvis operator and type casting precedence in Groovy

Posted by Jim White <ja...@gmail.com>.
Hi Ferenc,

I don't see anything about that in the release notes either and I tried
this search in JIRA (text=elvis && status=unresolved | fixed
<https://issues.apache.org/jira/browse/GROOVY-11014?jql=project%20%3D%20GROOVY%20AND%20resolution%20in%20(Unresolved%2C%20Fixed)%20AND%20text%20~%20%22elvis%22%20ORDER%20BY%20cf%5B12312521%5D%20DESC%2C%20priority%20DESC%2C%20updated%20DESC>)
which doesn't appear to have anything in the v3 vs v4 timeframe that looks
related.  It is quite plausible that the change involved wasn't specific to
the elvis operator and is described for some more general change to the
type system.  There is a recent open issue that talks about something
similar so maybe something has changed not too long ago:
https://issues.apache.org/jira/browse/GROOVY-11014

In any case I agree with the SO answer that the v4 behavior is correct
(i.e. the type of the elvis condition value should only be relevant when it
is truthy) but I'm not up to date on all the various
compiler type inference things in Groovy these days.

Notably there don't appear to be any type inference related tests for the
elvis operator:
https://github.com/apache/groovy/blob/95f36bc52cdca47ca7722e45aabb57152dcc86f7/src/test/groovy/operator/TernaryOperatorsTest.groovy#L55

Jim




On Mon, Aug 14, 2023 at 8:19 PM Ferenc Nagy <nf...@gmail.com> wrote:

> Hi Devs,
>
> I've posted a question about the topic here:
>
> https://stackoverflow.com/questions/76863969/elvis-operator-and-type-casting-precedence-in-groovy/76900266#76900266
>
> Just repeating the same here and I'm curious about your opinion. Should I
> open a bug ticket for this as suggested in the answer in SOF?
>
> ---
>
> Let's take the following simple expression:
>
> ((Double) null ?: 0).getClass()
>
> Results:
>
>    - Groovy 3: class java.lang.Double
>    - Groovy 4: class java.lang.Integer
>
> Does anyone know the reason for the different behaviour? I'd say Groovy 4
> is correct since the casting is applied before the Elvis operator.
>
> Checked, but couldn't find anything related in the Groovy 4 release notes:
> https://groovy-lang.org/releasenotes/groovy-4.0.html
>
>
> ---
>
>
> Regards,
>
> Ferenc
>