You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by MG <mg...@arscreat.com> on 2017/12/11 23:45:38 UTC

@CompileStatic null iteration

Hi guys,

just a quick question since we came across it today when testing code 
that had been converted from dynamic to static Groovy: Is the behavior 
that statically compiled Groovy throws a NPE when the iterable to be 
iterated over is null (same as Java), while dynamically compiled Groovy 
uses the NullObject.iterator(), i.e. does not throw but instead iterates 
over an empty collection by design ?

Cheers,
mg



Re: @CompileStatic null iteration

Posted by mg <mg...@arscreat.com>.
"If we want to keep static Groovy near Java as much as possible..." - who wants that ? ;-)
That is pretty much what I expected. So warning developers about this is the best thing to do.
Btw, do we already have an official "@CompileStatic gotchas" section somewhere ?
Cheers,mg


-------- Ursprüngliche Nachricht --------Von: Jochen Theodorou <bl...@gmx.org> Datum: 15.12.17  16:34  (GMT+01:00) An: users@groovy.apache.org Betreff: Re: @CompileStatic null iteration 

Am 15.12.2017 um 14:35 schrieb Winnebeck, Jason:
> It’s been like this for as long as I’ve been using compile static in 
> Groovy. I assumed that was expected behavior and I make sure to point it 
> out to all new developers in our project as one of the Groovy gotchas. 
> Is it not supposed to be that way?

Afaik it is supposed to be this way for technical reasons

Not failing invocation on null means to not be able to use direct method 
calls in static compiled code. And while they do not have to be as 
dynamic as in standard Groovy, you would have to make them at least with 
invokedynamic. And that is because if I tell the JVM to invoke 
String#length(), it does not like to get NullObject instead of a String.

in summary:
If we want to keep static Groovy near Java as much as possible, we pay 
this for example with the NPE here.

bye Jochen

Re: @CompileStatic null iteration

Posted by Jochen Theodorou <bl...@gmx.org>.
Am 15.12.2017 um 14:35 schrieb Winnebeck, Jason:
> It’s been like this for as long as I’ve been using compile static in 
> Groovy. I assumed that was expected behavior and I make sure to point it 
> out to all new developers in our project as one of the Groovy gotchas. 
> Is it not supposed to be that way?

Afaik it is supposed to be this way for technical reasons

Not failing invocation on null means to not be able to use direct method 
calls in static compiled code. And while they do not have to be as 
dynamic as in standard Groovy, you would have to make them at least with 
invokedynamic. And that is because if I tell the JVM to invoke 
String#length(), it does not like to get NullObject instead of a String.

in summary:
If we want to keep static Groovy near Java as much as possible, we pay 
this for example with the NPE here.

bye Jochen

RE: @CompileStatic null iteration

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
It’s been like this for as long as I’ve been using compile static in Groovy. I assumed that was expected behavior and I make sure to point it out to all new developers in our project as one of the Groovy gotchas. Is it not supposed to be that way?

Jason

From: Paolo Di Tommaso [mailto:paolo.ditommaso@gmail.com]
Sent: Friday, December 15, 2017 2:29 AM
To: users@groovy.apache.org
Subject: Re: @CompileStatic null iteration

Just found the same problem. This difference can introduce subtle bugs when refactoring groovy code to CompileStatic.

I suggest to report a bug for that.


Cheers, Paolo


On Tue, Dec 12, 2017 at 12:45 AM, MG <mg...@arscreat.com>> wrote:
Hi guys,

just a quick question since we came across it today when testing code that had been converted from dynamic to static Groovy: Is the behavior that statically compiled Groovy throws a NPE when the iterable to be iterated over is null (same as Java), while dynamically compiled Groovy uses the NullObject.iterator(), i.e. does not throw but instead iterates over an empty collection by design ?

Cheers,
mg



This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.

Re: @CompileStatic null iteration

Posted by Paolo Di Tommaso <pa...@gmail.com>.
Just found the same problem. This difference can introduce subtle bugs when
refactoring groovy code to CompileStatic.

I suggest to report a bug for that.


Cheers, Paolo


On Tue, Dec 12, 2017 at 12:45 AM, MG <mg...@arscreat.com> wrote:

> Hi guys,
>
> just a quick question since we came across it today when testing code that
> had been converted from dynamic to static Groovy: Is the behavior that
> statically compiled Groovy throws a NPE when the iterable to be iterated
> over is null (same as Java), while dynamically compiled Groovy uses the
> NullObject.iterator(), i.e. does not throw but instead iterates over an
> empty collection by design ?
>
> Cheers,
> mg
>
>
>