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 <nferi....@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
>

Reply via email to