You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2020/11/11 02:02:05 UTC

[jira] [Closed] (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 ]

Paul King closed GROOVY-7409.
-----------------------------

> 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
>            Priority: Critical
>
> 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
(v8.3.4#803005)