Guillaume Laforge | 2 May 2005 14:16
Picon
Gravatar

With (continued, and the use of 'delegate')

Hello,

I ceize the opportunity of the "with" keyword debate to make a few remarks:

1) Groovy's pretty powerful and self-evolutive
2) There's some uses for the "delegate" notion in closures

So, why do I say that Groovy is pretty powerful and self-evolutive?
Because of its Metaobject protocol facilities (property setter/getter
and invoke method interception) and its extensibility with regard to
adding new methods on standard Java objects (ie. Groovy JDK).

Following the idea of the "with" keyword, and taking advantage of
Groovy's power and extensibility, Jeremy implemented a new "identity"
method on Object -- thus available to any object. In CVS Head, you can
do something like that:

foo[i].bar().film.identity { f -> 
   f.name = "Lavender Hill Mob" 
   f.year = 1951 
}

You've got a complex expression, and without having to repeat each
time that long expression or assigning it to some temporary variable,
you can use a closure to assign some values to the object's
properties.

Jeremy rocks :-)

Though John Rose and James seems to want to abandon the "delegate" of
(Continue reading)

Dierk Koenig | 2 May 2005 14:37
Favicon
Gravatar

RE: With (continued, and the use of 'delegate')

> -----Original Message-----
> From: Guillaume Laforge [mailto:glaforge@...]
> Sent: Montag, 2. Mai 2005 14:17
> To: dev@...; jsr@...

How does 

> foo[i].bar().film.identity { f -> 
>    f.name = "Lavender Hill Mob" 
>    f.year = 1951 
> }

compare to

 f = foo[i].bar().film 
 f.name = "Lavender Hill Mob" 
 f.year = 1951 

?

Is the "localization" of "f" the only
difference? 
Is it worth while regarding all the additional
thoughts about scope, exit handling, etc.?

cheers
Mittie

Guillaume Laforge | 2 May 2005 15:22
Picon
Gravatar

Re: With (continued, and the use of 'delegate')

> How does
> 
> > foo[i].bar().film.identity { f ->
> >    f.name = "Lavender Hill Mob"
> >    f.year = 1951
> > }
> 
> compare to
> 
> f = foo[i].bar().film
> f.name = "Lavender Hill Mob"
> f.year = 1951
> 
> ?
>
> Is the "localization" of "f" the only
> difference?

Just a matter of taste ? :-)
Compared to my third example though, it's even cuter than that second one.

> Is it worth while regarding all the additional
> thoughts about scope, exit handling, etc.?

There are much more important things than that. I agree.

My point was twofold: 

1) I just wanted to stress that certain features such as "with" are
easily "implementable" thanks to Groovy's MOP and its intrinsic
(Continue reading)

Dierk Koenig | 2 May 2005 16:17
Favicon
Gravatar

RE: With (continued, and the use of 'delegate')

> -----Original Message-----
> From: Guillaume Laforge [mailto:glaforge@...]
..
> 1) I just wanted to stress that certain features such as "with" are
> easily "implementable" thanks to Groovy's MOP and its intrinsic
> extensibility (thus we can live without "with" since we can mock it
> easily)

agreed.

> 2) I also wanted to show that Closure "delegates" have some 
> interesting uses.

Yes, but they come at an expense such that we have a trade-off.
In my eyes the expense is in a higher complexity and (at least
for me) an element of "surprise". 

cheers
Mittie

Guillaume Laforge | 2 May 2005 16:50
Picon
Gravatar

Re: With (continued, and the use of 'delegate')

> Yes, but they come at an expense such that we have a trade-off.
> In my eyes the expense is in a higher complexity and (at least
> for me) an element of "surprise".

Well, I quite like that feature on the contrary, you can change the
scope dynamically, which is kinda pretty powerful.
It's better to be surprised because something work, rather than
because something doesn't work as you expected: you're glad your
closure find your variables, but you're sad if it can't :-)

--

-- 
Guillaume Laforge
http://glaforge.free.fr/weblog/?catid=2

Dierk Koenig | 2 May 2005 17:14
Favicon
Gravatar

RE: With (continued, and the use of 'delegate')

I understand that you prefer to pay the expense of complexity and
surprise (if any) in order to gain functionality and
flexibility.

cheers
Mittie

> -----Original Message-----
> From: Guillaume Laforge [mailto:glaforge@...]
> Sent: Montag, 2. Mai 2005 16:50
> To: jsr@...
> Subject: Re: [groovy-jsr] With (continued, and the use of 'delegate')
> 
> 
> > Yes, but they come at an expense such that we have a trade-off.
> > In my eyes the expense is in a higher complexity and (at least
> > for me) an element of "surprise".
> 
> Well, I quite like that feature on the contrary, you can change the
> scope dynamically, which is kinda pretty powerful.
> It's better to be surprised because something work, rather than
> because something doesn't work as you expected: you're glad your
> closure find your variables, but you're sad if it can't :-)
> 
> -- 
> Guillaume Laforge
> http://glaforge.free.fr/weblog/?catid=2
> 

(Continue reading)

Guillaume Laforge | 2 May 2005 17:15
Picon
Gravatar

Re: With (continued, and the use of 'delegate')

On 02/05/05, Dierk Koenig <dierk.koenig@...> wrote:
> I understand that you prefer to pay the expense of complexity and
> surprise (if any) in order to gain functionality and
> flexibility.

Exactly :-)

I want to be able to write cool stuff, like Scriptom, some cool
proxying objects, some new language constructs without requiring
syntax changes to the language, etc.

--

-- 
Guillaume Laforge
http://glaforge.free.fr/weblog/?catid=2

John Rose | 2 May 2005 22:01
Picon
Favicon

Re: With (continued, and the use of 'delegate')

On May 2, 2005, at 6:22, Guillaume Laforge wrote:
> 1) I just wanted to stress that certain features such as "with" are
> easily "implementable"
> 2) I also wanted to show that Closure "delegates" have some 
> interesting uses.

We agree that scope warping is sometimes useful.
("Scope warping" = suspending the usual lookup rule for unqualified 
names.)
Builders and other embedded languages can use this to simplify their 
notation.

Your two points are interconnected.
It's true that if we were to keep delegates, we could implement 'with' 
as a method.
The reverse is true, also:
The 'with' statement will give the power of a delegate to any block 
that needs it.
The difference between the approaches is one of design and taste, not 
power.

Delegates have a much larger impact on the language, because
the delegate feature potentially warps the scope of any closed block.

The 'with' statement makes such warping much more explicit,
exhibiting the delegate object at the beginning of the warped scope.

Because of the fundamental nature of unqualified names in Groovy
(in any programming language, really), I prefer to make their scoping
to be sign-posted explicitly.
(Continue reading)

Guillaume Laforge | 2 May 2005 22:36
Gravatar

Re: With (continued, and the use of 'delegate')

Guillaume Laforge wrote:
> [...]
> But we can even do better than the little example above, thanks to
> that "delegate" hack on Closures. If the "identity" method sets the
> delegate to be equal to the implicit parameter of the closure, you can
> even write:
> 
> foo[i].bar().film.identity { 
>    name = "Lavender Hill Mob" 
>    year = 1951 
> }
> 
> That's exactly what "with" is about :-)
> 
> I haven't commited that, but I may well do it soon. 
> You can even mimic that behaviour by passing a specific closure like:
> 
> foo[i].bar().film.identity { 
>    this.delegate = it
>    name = "Lavender Hill Mob" 
>    year = 1951 
> }
> [...]

I've commited the use of the closure delegate, which allows us to write 
that:

        def a = new Expando()
        a.foobar = "foobar"
        a.barfoo = 555
(Continue reading)

John Rose | 2 May 2005 22:58
Picon
Favicon

Re: Rules for scoping

On Apr 27, 2005, at 3:40, Jochen Theodorou wrote:
> Ok, let us first list some points where we agree:
>
> * we want to compiler checks where possible
> * we do basically the same scoping rules as java does
> * we do not want to change the syntax of closures
> * a variable declared inside a closure is not allowed to shadow
> * "{" for closures without "->" does introduce a typeless parameter 
> "it"
> * "{->" does not bind 'it' and creates a parameterless closure
Fantastic summary!
Enthusiastic agreement on these points, with this harmless limitation:
> * "{" for closures without "->" does introduce a typeless parameter 
> "it", if "it" is used
It's the old paradox of "if a tree falls in the forest, does it make a 
sound?"
If the name "it" does not occur in the expression, then we language 
designers
have a choice whether the rules will require it to be bound or not.

>> On Apr 25, 2005, at 5:46, Jochen Theodorou wrote:
> [...]
>>> ...The shortcut
>>> "{" without "->" introduces the typeless optional parameter 'it', and
>>> only that one is a possible parameter to the closure.
>> I think we all agree on this; it is as if the user had written {def 
>> it=null->s} instead of {s}.
> Sorry, about this, but if the points we agree about are right, then 
> "{def it=null->s}" is only allowed if there is no case of shadowing, 
> where "{s}" would allow the usage of it.
(Continue reading)


Gmane