groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Rüegg <rueegg.mich...@gmail.com>
Subject Re: Strange behaviour with binding resolution in Groovy based DSL
Date Mon, 26 Sep 2016 08:41:52 GMT
Hi Jochen,

You are right, there is no need to use rehydrate in this example. Keeping track of the owner
and delegate in this nested hierarchy would complicate my code and so I will just use a plain
delegate as follows which lets me access the binding in DSL_NOT_OK:

Foo dslEntryPoint(@DelegatesTo(Foo) closure) {
  Foo foo = new Foo()
  closure.delegate = foo
  closure.call()
  foo
}

Thanks,
Michael

> On 26 Sep 2016, at 10:30, Jochen Theodorou <blackdrag@gmx.org> wrote:
> 
> 
> 
> On 26.09.2016 09:02, Michael Rüegg wrote:
>> Hi,
>> 
>> I have a Groovy based DSL in which I want to access variables from declared bindings.
Here's my DSL implementation:
>> 
>> https://gist.github.com/MichaelRueegg/f3f09aeb5853bdb58ca017eb1d4d7a30
>> 
>> As you can see, when I bind the variable magicValue with def (DSL_OK), I can access
its value in my DSL, whereas otherwise (DSL_NOT_OK) I get a
>> 
>> java.lang.NullPointerException: Cannot get property 'magicValue' on null object
> 
> in the first case you are accessing a local variable, which is done without bothering
with owner and delegate of the Closure instance. In the second case you are going through
these to find the meaning of the magicValue. But since you use rehydrate to set owner and
delegate to null, you will end up in an error.  Why do you do rehydrate at all? Maybe in your
bigger code there is a need for this I won't see here, but in what I see I obviously fail
to see the need. If you have to use it, then of course you will have to set owner and/or delegate,
or else you won't be able to sue the standard mechanisms to access the binding from the Closure
instance
> 
> bye Jochen


Mime
View raw message