Question

I have something like the following groovy class :

class Foo {
   private Map<String,String> bar = [:]

   Map<String, String> getBar() {
       return bar.asImmutable()
   }

   def doSomething(List<String> argValues){
     argValues.each {
        bar[it] = it
     }
   }
}

The doSomething method will fail, it seem, in the each closure, the bar property is use trough accessor, not the field. So it's immutable.

The question is "how can i use the field (not the accessor) within the closure ?

Thanks.

Was it helpful?

Solution

You can use the property accessor operator @ like so:

this.@bar[ it ] = it 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top