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 2022/02/03 22:50:00 UTC

[jira] [Closed] (GROOVY-10241) Class Implementing Map Can't Read Fields From Within Closures

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

Paul King closed GROOVY-10241.
------------------------------

> Class Implementing Map Can't Read Fields From Within Closures
> -------------------------------------------------------------
>
>                 Key: GROOVY-10241
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10241
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.9
>            Reporter: Ian
>            Priority: Major
>
> If a class implements 'Map' or subclasses an implementation of 'Map' then closures can't read fields in that class. This code is in a script called 'map.groovy':
>  
> {noformat}
> class A extends HashMap {
>   @Override
>   Object get(Object key) {
>     return 'OtherString'
>   }
> }
> class B extends A {
>   String aa = 'TheString'    
>   def foo() {
>     println aa
>     Closure c = {println aa}
>     c.call()
>   }
> }
> new B().foo(){noformat}
> {noformat}
> $ groovy map.groovy 
> TheString
> OtherString{noformat}
> I expect to get "TheString" twice.
>  
> I see the same behavior with other implementations of 'Map' or when I implement 'Map' directly.
> Curiously I was not able to get the same behavior when I created my own interface defining 'get()' and implementing that.
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)