[ https://issues.apache.org/jira/browse/GROOVY-7409?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
paolo di tommaso resolved GROOVY-7409. -------------------------------------- Resolution: Not A Bug Yes. Closing as not a bug. Quite tricky tho. > 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 (v7.6.3#76005)