groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Closure does not see field when setting value
Date Tue, 08 Nov 2016 13:29:11 GMT
On 08.11.2016 13:43, Paul King wrote:
> Jochen, indeed the Closure version goes through setProperty() but I
> thought we made it work in methods:
>
> import groovy.transform.Field
>
> @Field
> int x = 2
>
> def a() {
>     assert x == 2
>     println x
>     x = 1
>     assert x == 1
>     println x
> }
>
> println x
> binding.variables.remove('x')
> a()
> assert x == 1
> println x
> println binding.variables.x
>
> but not closures?

if there is a field of name x, we do directly access the field for the 
implicit or explicit this.... unless it is an open block, there we 
access the field only directly if part of the Closure, or an explicit 
this is used. For property access that happens on something else... for 
example foo.x ... we also do always go through the MOP.

bye Jochen

Mime
View raw message