antoaravinth | 16 Aug 2012 08:00
Picon
Gravatar

[groovy-user] Exact difference between Script class and Binding class in groovy.

Consider this code:

x = 10
10.times{    
    x++
}
assert ​binding.getVariable("x") == 20
As far, I'm concerned this is what happening in the above code: Script class
has a reference to the variable x which has value 10 and it calls the times
closure which will access the variable x from Script. So basically the
scoping of x is been done by Script class.

But I wonder when binding came into the picture? In sense, I haven't called
new Binding() or anything, but still its there.

I'm bit confused with it. What is the exact use of binding? Does Script
class creates binding, if so that is how it manages scoping of x within
times method?

Thanks in advance.

--
View this message in context: http://groovy.329449.n5.nabble.com/Exact-difference-between-Script-class-and-Binding-class-in-groovy-tp5710950.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

(Continue reading)

Jochen Theodorou | 16 Aug 2012 09:25
Picon
Gravatar

Re: [groovy-user] Exact difference between Script class and Binding class in groovy.

Am 16.08.2012 08:00, schrieb antoaravinth:
> Consider this code:
>
> x = 10
> 10.times{
>      x++
> }
> assert ​binding.getVariable("x") == 20
> As far, I'm concerned this is what happening in the above code: Script class
> has a reference to the variable x which has value 10 and it calls the times
> closure which will access the variable x from Script. So basically the
> scoping of x is been done by Script class.
>
> But I wonder when binding came into the picture? In sense, I haven't called
> new Binding() or anything, but still its there.

Ok... You code above is a subclass of Script, Script has a parameterless 
constructor Script() which creates a new Binding for this script. If the 
code above is executed from the command line for example, this way is 
used to set the Binding. Now, in your example above, "x" is not part of 
the Script subclass, instead it is an information stored in the Binding 
of the Script. The scoping of x is the binding, which is in your case 
the whole script. But you could share bindings among several scripts, in 
which case the scoping would be more than just the script and you would 
have something like a global variable.

> I'm bit confused with it. What is the exact use of binding? Does Script
> class creates binding, if so that is how it manages scoping of x within
> times method?

(Continue reading)

aravinth | 16 Aug 2012 19:09
Picon

[groovy-user] Re: Exact difference between Script class and Binding class in groovy.

Excellent explanation. Thanks a lot. So one more doubt. Delegate of a closure
is the one who hold's the closure, right?

--
View this message in context: http://groovy.329449.n5.nabble.com/Exact-difference-between-Script-class-and-Binding-class-in-groovy-tp5710950p5710955.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Aug 2012 08:47
Picon
Gravatar

Re: [groovy-user] Re: Exact difference between Script class and Binding class in groovy.

Am 16.08.2012 19:09, schrieb aravinth:
> Excellent explanation. Thanks a lot. So one more doubt. Delegate of a closure
> is the one who hold's the closure, right?

Ah, Groovy and Closure vs. closure.... long chapter...

The delegate is part of the dynamic property lookup done using the MOP. 
If set and depending on the resolve strategy used we for example first 
ask the delegate to resolve the property instead of the surrounding 
class. The delegate is optional, so not the closure.

Not every Groovy Closure instance is a closure in terms of a functional 
lexical closure. For example:

int i = 10
[1,2,3].each {println it+i}

would be such a lexical closure once created. The closure itself is no 
object, it is the process of binding the variables from the context. 
Since in

[1,2,3].each {println it+i}

the context for i is missing, this cannot happen once, but is done using 
a dynamic scoping and thus you cannot have that kind of closure here. 
The two examples differ for example in the class constructor we generate 
for that. In the first case i will be wrapped in a groovy.lang.Reference 
and given to the Closure subclass to have a shared variable. No dynamic 
lookup/scoping will be done for it. In the second case the constructor 
is short by that parameter and the lookup is instead done by the MOP and 
(Continue reading)

antoaravinth | 17 Aug 2012 09:58
Picon
Gravatar

[groovy-user] Re: Exact difference between Script class and Binding class in groovy.

Thanks for your reply's. Brilliant. 

--
View this message in context: http://groovy.329449.n5.nabble.com/Exact-difference-between-Script-class-and-Binding-class-in-groovy-tp5710950p5710969.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane