You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "paolo di tommaso (JIRA)" <ji...@apache.org> on 2015/05/01 06:55:08 UTC

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

     [ 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 which 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)