groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Closure does not see field when setting value
Date Tue, 08 Nov 2016 11:19:23 GMT
I think that is probably a bug - but with reads and writes. Change
your declaration to "int x = 2" and see what you get?

Cheers, Paul.


On Tue, Nov 8, 2016 at 8:54 PM, Krzysztof Kowalczyk
<kowalczyk.krzysztof@gmail.com> wrote:
> Hi,
> Today I was quite confused by behaviour of fields inside of closures. When I
> read a field value it is visible, but when I try to set it, it is not set,
> but a binding is set and then available for rest of the closure. This is
> rather confusing, can I disable such behaviour in any way?
>
> import groovy.transform.Field
>
> @Field
> int x = 0
>
> a {
>     assert x == 0
>     println x
>     x = 1
>     assert x == 1
>     println x
> }
>
> assert x == 0
> println x
>
> def a(Closure c) {
>     c()
> }
>
>
> ________________________________
> View this message in context: Closure does not see field when setting value
> Sent from the Groovy Users mailing list archive at Nabble.com.

Mime
View raw message