You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Jochen Eddelbuettel (Jira)" <ji...@apache.org> on 2020/05/17 11:29:00 UTC

[jira] [Comment Edited] (GROOVY-9554) @Field variable access within closures broken

    [ https://issues.apache.org/jira/browse/GROOVY-9554?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17109436#comment-17109436 ] 

Jochen Eddelbuettel edited comment on GROOVY-9554 at 5/17/20, 11:28 AM:
------------------------------------------------------------------------

I guess this [blog post|[https://e.printstacktrace.blog/groovy-script-closure-does-not-modify-field-annotated-variable/]] kind of explains that I'm looking at a bug that is deemed a feature, right?

If @Field could generate a getter and a setter, that would allow forcing accesss to the field. An explicit call to the getter or setter would prevent the shadowing by the binding, which is the approach I've taken to make sure I'm reaching my @BaseScript defined properties.

How is that for a future improvement? It could even be a separate AST Annotation, e.g. @PropertyField or @BeanField.


was (Author: eddelplus):
I guess this [blog post|[https://e.printstacktrace.blog/groovy-script-closure-does-not-modify-field-annotated-variable/]] kind of explains that I'm looking at a bug that is deemed a feature, right?

If @Field could generate a getter and a setter, that would allow forcing accesss to the field. An explicit call to the getter or setter would prevent the shadowing by the binding, which is the approach I've taken to make sure I'm reaching by @BaseScript defined properties.

How is that for a future improvement? It could even be a separate AST Annotation, e.g. @PropertyField.

> @Field variable access within closures broken
> ---------------------------------------------
>
>                 Key: GROOVY-9554
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9554
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.15, 3.0.3
>            Reporter: Jochen Eddelbuettel
>            Priority: Major
>
>  
> {code:java}
> @groovy.transform.Field String abc
> binding.variables.clear()
> abc = "abc"
> println binding.hasVariable("abc")
> ["D","E","F"].each { abc += it }
> println binding.getVariable("abc")
> assert abc == "abcDEF"
> {code}
> If a variable is declared using the @Field annotation, it can be assigned to in the main script body, but any assignment inside a closure (here the closure used with each) ends up in the script binding instead. Assignments within regular methods work fine, too. If the variables is accessed inside the closure and it is not in the binding, then the value will come from the field. As soon as it is in the binding, the binding variable takes precedence over the field. In both cases the field declared with @Field needs to have precedence over anything that is in the binding, just like local variables do.
>   
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)