John Wagenleitner commented on GROOVY-8275:

For #2 and #3, looks like adding local variable support was part of GROOVY-4876 
as a way to [enable usage in 
  Based on GROOVY-4876 I'm not sure if this is by design; that the presence of 
the annotation on a local variable turns it on for all others in the script.  
For example:

def x = Integer.new(1)
def y = Integer.new(2)
def z = Integer.new(3)

assert x + y + z == 6

It does lead to unexpected results when used in a Class.

class Foo {
    int bar() {
        def x = Integer.new(1)
    int baz() {
        def y = Integer.new(2)
assert new Foo().bar() == 1
assert new Foo().baz() == 2

> Newify transform docs missing support for local variable declaration
> --------------------------------------------------------------------
>                 Key: GROOVY-8275
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8275
>             Project: Groovy
>          Issue Type: Documentation
>          Components: xforms
>            Reporter: Eric Milles
>            Priority: Minor
> Javadocs and online documentation for the Newify AST transform is not quite 
> clear in a couple instances.
> 1) Online docs http://groovy-lang.org/metaprogramming.html#xform-Newify 
> Ruby-style section could be more clear IMO if the {{([Tree,Leaf])}} were 
> removed since it only enables the Python-style expressions.
> 2) Javadoc and online docs do not mention that {{@Newify}} can be applied to 
> a local variable declaration, like: {{@Newify def o = Object.new()}}.
> 3) The scoping of the transform is pretty clear for the class, method and 
> field/property cases.  However for the local variable case, it appears that 
> the transform applies throughout the containing scope of the local variable.  
> That is, I can use the extra constructors anywhere in the block to 
> re-initialize the variable.  {{@Newify def o = Object.new(); ... o = 
> Object.new() ...}}.  Is this the intended behavior?  My intuition was that 
> the initializer expression was the only place the transform applied, like in 
> the case of a field.

This message was sent by Atlassian JIRA

Reply via email to