[jira] [Updated] (GROOVY-7409) Closure reference a wrong object when is defining inside an iterator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Updated] (GROOVY-7409) Closure reference a wrong object when is defining inside an iterator

JIRA jira@apache.org

     [ https://issues.apache.org/jira/browse/GROOVY-7409?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

paolo di tommaso updated GROOVY-7409:
-------------------------------------
    Description:
A closure defined inside an iterator containing a reference to the iterating item resolves a wrong object instance.

To reproduce the error take in consideration the following snippet:

{code}
interface Alpha {
  abstract void m()
}

class Foo implements Alpha {
  void m() { println 'foo' }
}

class Bar implements Alpha {
  void m() { println 'bar' }
}

List list = [new Foo(), new Bar()]

def hooks = []
for( def item : list ) {
  hooks.add { item.m() }
}

hooks.each { it.call() }
{code}

It prints

{code}
bar
bar
{code}

Replacing the `for` iterator with a `for( int i=0; etc ) .. ` prints correctly:

{code}
foo
bar
{code}

  was:
A closure defined inside an iterator containing a reference to the iterating item resolves a wrong object instance.

To reproduce the error take in consideration the following snippet:

{code}
interface Alpha {
  abstract void m()
}

class Foo implements Alpha {
  void m() { println 'foo' }
}

class Bar implements Alpha {
  void m() { println 'bar' }
}

List list = [new Foo(), new Bar()]

def hooks = []
for( def item : list ) {
  hooks.add { item.m() }
}

hooks.each { it.call() }
{code}

It prints

{code}
bar
bar
{code}

Replacing the `for` iterator with a `for( int i=0; etc ) { .. }` prints correctly:

{code}
foo
bar
{code}


> Closure reference a wrong object when is defining inside an iterator
> ---------------------------------------------------------------------
>
>                 Key: GROOVY-7409
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7409
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.3.11, 2.4.3
>            Reporter: paolo di tommaso
>
> A closure defined inside an iterator containing a reference to the iterating item resolves a wrong object instance.
> To reproduce the error take in consideration the following snippet:
> {code}
> interface Alpha {
>   abstract void m()
> }
> class Foo implements Alpha {
>   void m() { println 'foo' }
> }
> class Bar implements Alpha {
>   void m() { println 'bar' }
> }
> List list = [new Foo(), new Bar()]
> def hooks = []
> for( def item : list ) {
>   hooks.add { item.m() }
> }
> hooks.each { it.call() }
> {code}
> It prints
> {code}
> bar
> bar
> {code}
> Replacing the `for` iterator with a `for( int i=0; etc ) .. ` prints correctly:
> {code}
> foo
> bar
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)