Thanks Jochen and Christopher for helping clarify behaviour. So if I wanted
to make sure declaration and not flow type is honored, the quickest way is
to make sure flow type matches declaration by casting it? Are there other
ways inside the compiler to fix this (some options or some indications in
the variable declaration statement itself) (I looked and didnt find
anything, but thought I'd ask)?

I am using 3.0.5 and I compile statically into the file system

regards
Saravanan

On Sun, Dec 4, 2022 at 12:25 AM Jochen Theodorou <blackd...@gmx.org> wrote:

> On 03.12.22 15:50, Christopher Smith wrote:
> > I believe the feature at play is "flow typing", and it surprises me that
> > it would apply to variables declared with an explicit type. What version
> > of Groovy are you targeting, and is this compiled statically or
> > dynamically?
>
> The declaration type is to be understood as an base type for the
> variable, not the flow type. Which means in
>
> Y foo = bar()
> foo.x()
>
> the flow type of foo is whatever is inferred as result type for the call
> bar(). The assignment to foo is valid if the result type for the call to
> bar() is assignable to Y. The call x() on foo is valid if the method x()
> exists on the flow type.
>
> This is the intended behaviour.
>
> bye Jochen
>
>

Reply via email to