Compilationerror for using "it" in a Closure without "it"?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Compilationerror for using "it" in a Closure without "it"?

Jochen Theodorou
Hi,

it is the following problem:

def c = {
   def d = {->println it}
   d()
}
c()

should this program be compileable? Should it throw a
MissingPropertyException?

The problem is that the outer closure defines an implicit "it" which is
then used inside the inner closure. The inner Closure doesn't have a
parameter, so it can only be the outer parameter. But changing this code
a little:

def c = {
   def d = {println it}
   d()
}
c()

now the inner closure defines the parameter "it" and not the outer. But
what "it" was ment?

I know we had a little discussion about that, but not really with a
result. Making this first case a compile time error would mean that "it"
can never be a dynamic name inside a closure, but should it really be
the outer closures parameter? I think not being able to use "it" here is ok.

What do you giys think?

bye blackdrag
Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] Compilationerror for using "it" in a Closure without "it"?

Dierk König
I feel like the innermost 'it' should be enough.

Nested usages can occur fairly often with a mix of
builders, iterations, and GPath expressions. If you need
access to an outer 'it' I think it's ok that you have
to declare that variable.

-> I think we should disallow access to an outer 'it'.

The trouble below doesn't come only from the nesting but
from {-> ...} disabling 'it', which gets in conflict
if there's an outer one...

cheers
Mittie

> -----Original Message-----
> From: Jochen Theodorou [mailto:[hidden email]]
> Sent: Montag, 30. Januar 2006 15:06
> To: [hidden email]; [hidden email]
> Subject: [groovy-dev] Compilationerror for using "it" in a Closure
> without "it"?
>
>
> Hi,
>
> it is the following problem:
>
> def c = {
>    def d = {->println it}
>    d()
> }
> c()
>
> should this program be compileable? Should it throw a
> MissingPropertyException?
>
> The problem is that the outer closure defines an implicit "it" which is
> then used inside the inner closure. The inner Closure doesn't have a
> parameter, so it can only be the outer parameter. But changing this code
> a little:
>
> def c = {
>    def d = {println it}
>    d()
> }
> c()
>
> now the inner closure defines the parameter "it" and not the outer. But
> what "it" was ment?
>
> I know we had a little discussion about that, but not really with a
> result. Making this first case a compile time error would mean that "it"
> can never be a dynamic name inside a closure, but should it really be
> the outer closures parameter? I think not being able to use "it"
> here is ok.
>
> What do you giys think?
>
> bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: Compilationerror for using "it" in a Closure without "it"?

Guillaume Laforge
Administrator
In reply to this post by Jochen Theodorou
I think that "it" should only refer to the implicit parameter of the
current closure and should probably not be used as an identifier when
inside a closure.
"it" should not refer to an implicit parameter from an outer closure.
In case we want to pass that implicit parameter, I think it's safer
and easier to read if the parameter is aliased in another variable.

In your example:

def c = {
  def d = {->println it}
  d()
}
c()

It shouldn't compile, because the inner closure has no parameter, so
it can't have an implicit parameter.

If the intent of the code is to use the outer "it" implicit parameter
from the outer closure, then the developer should write:

def c = {
  def param = it
  def d = {println param}
  d()
}
c()

Does it make sense?

I think at the meeting or before, we decided that "it" should always
refer to the current closure it's used in. It should never refer to
another "it" from an outer closure.


On 30/01/06, Jochen Theodorou <[hidden email]> wrote:

> Hi,
>
> it is the following problem:
>
> def c = {
>    def d = {->println it}
>    d()
> }
> c()
>
> should this program be compileable? Should it throw a
> MissingPropertyException?
>
> The problem is that the outer closure defines an implicit "it" which is
> then used inside the inner closure. The inner Closure doesn't have a
> parameter, so it can only be the outer parameter. But changing this code
> a little:
>
> def c = {
>    def d = {println it}
>    d()
> }
> c()
>
> now the inner closure defines the parameter "it" and not the outer. But
> what "it" was ment?
>
> I know we had a little discussion about that, but not really with a
> result. Making this first case a compile time error would mean that "it"
> can never be a dynamic name inside a closure, but should it really be
> the outer closures parameter? I think not being able to use "it" here is ok.
>
> What do you giys think?
>
> bye blackdrag
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: [groovy-jsr] Compilationerror for using "it" in a Closure without "it"?

Russel Winder
An alternative is not to allow an implicit parameter anywhere but to
always require all closure parameters to be declared.

I have to admit I always do in my code style.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: Compilationerror for using "it" in a Closure without "it"?

graemer
On 30/01/06, Russel Winder <[hidden email]> wrote:
> An alternative is not to allow an implicit parameter anywhere but to
> always require all closure parameters to be declared.

No i disagree with this as it is not always possible to know the name
of the closure parameter at runtime especially when you closures are
dynamically generated. We use this technique in Grails for dynamic
taglibs

Graeme

>
> I have to admit I always do in my code style.
> --
> Russel.
> ====================================================
> Dr Russel Winder                +44 20 7585 2200
> 41 Buckmaster Road              +44 7770 465 077
> London SW11 1EN, UK             [hidden email]
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQBD3jBur2EGkixYSboRAkhFAKCNWhfL3G1S0bSFZyDtflmmoQOGrwCeL8z5
> pR3U0eV/wr8hvrzE576C3nM=
> =abve
> -----END PGP SIGNATURE-----
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: [groovy-jsr] Compilationerror for using "it" in a Closure without "it"?

Russel Winder
On Mon, 2006-01-30 at 15:35 +0000, Graeme Rocher wrote:
> On 30/01/06, Russel Winder <[hidden email]> wrote:
> > An alternative is not to allow an implicit parameter anywhere but to
> > always require all closure parameters to be declared.
>
> No i disagree with this as it is not always possible to know the name
> of the closure parameter at runtime especially when you closures are
> dynamically generated. We use this technique in Grails for dynamic
> taglibs

Perhaps I am being stupid but I don't see the problem you have.  Perhaps
you need to explain the actual technique to me a bit more !

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: Compilationerror for using "it" in a Closure without "it"?

graemer
On 30/01/06, Russel Winder <[hidden email]> wrote:

> On Mon, 2006-01-30 at 15:35 +0000, Graeme Rocher wrote:
> > On 30/01/06, Russel Winder <[hidden email]> wrote:
> > > An alternative is not to allow an implicit parameter anywhere but to
> > > always require all closure parameters to be declared.
> >
> > No i disagree with this as it is not always possible to know the name
> > of the closure parameter at runtime especially when you closures are
> > dynamically generated. We use this technique in Grails for dynamic
> > taglibs
>
> Perhaps I am being stupid but I don't see the problem you have.  Perhaps
> you need to explain the actual technique to me a bit more !

Essentially grails supports dynamic tag libraries (see
grails.codehaus.org/Dynamic+Tag+Libraries) these allow you to create
tags as if they were just javabean setters....

The body of the tag gets passed to the tag as a closure.. the tag then
can optionally pass a value to the closure which may be used for
things like iterative tags.. in addition they support both JSP and GSP
views and JSP requires you to declare the variable passed to the body
in the taglib TLD. Hence I declared it as "it".

You can then do iterative tags like:

<gr:each in="${people}">
      <p>${it.firstName}</p>
</gr:each>

Hope that helps you to understand :-)

>
> --
> Russel.
> ====================================================
> Dr Russel Winder                +44 20 7585 2200
> 41 Buckmaster Road              +44 7770 465 077
> London SW11 1EN, UK             [hidden email]
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQBD3jP8r2EGkixYSboRAngqAJ9O9R635jrvlw8kQB2hN4O3U3KlLQCfeoqo
> 1e0LY9CtrGnRIoShWo/AYws=
> =Hb05
> -----END PGP SIGNATURE-----
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: [groovy-jsr] Compilationerror for using "it" in a Closure without "it"?

Russel Winder
On Mon, 2006-01-30 at 16:07 +0000, Graeme Rocher wrote:

> Essentially grails supports dynamic tag libraries (see
> grails.codehaus.org/Dynamic+Tag+Libraries) these allow you to create
> tags as if they were just javabean setters....
>
> The body of the tag gets passed to the tag as a closure.. the tag then
> can optionally pass a value to the closure which may be used for
> things like iterative tags.. in addition they support both JSP and GSP
> views and JSP requires you to declare the variable passed to the body
> in the taglib TLD. Hence I declared it as "it".
>
> You can then do iterative tags like:
>
> <gr:each in="${people}">
>       <p>${it.firstName}</p>
> </gr:each>
>
> Hope that helps you to understand :-)
'Fraid not, it blew my head in.  I think I need more paracetamol to
fight this viral infection -- programming Groovy isn't doing the job :-)

I guess I need to play with Ruby on Rails and Grails from scratch toi
see how this computational model works currently it just looks like
overly convoluted mixing of XML and Groovy.  My problem though.

I have a need of creating a small database / finance transaction related
Web site to try out a business idea.  It took me 3ns to reject PHP, the
job is not big enough for Java per se so I had decided to use Ruby on
Rails.  Perhaps I should use Grails?
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Re: Compilationerror for using "it" in a Closure without "it"?

graemer
On 30/01/06, Russel Winder <[hidden email]> wrote:

> On Mon, 2006-01-30 at 16:07 +0000, Graeme Rocher wrote:
>
> > Essentially grails supports dynamic tag libraries (see
> > grails.codehaus.org/Dynamic+Tag+Libraries) these allow you to create
> > tags as if they were just javabean setters....
> >
> > The body of the tag gets passed to the tag as a closure.. the tag then
> > can optionally pass a value to the closure which may be used for
> > things like iterative tags.. in addition they support both JSP and GSP
> > views and JSP requires you to declare the variable passed to the body
> > in the taglib TLD. Hence I declared it as "it".
> >
> > You can then do iterative tags like:
> >
> > <gr:each in="${people}">
> >       <p>${it.firstName}</p>
> > </gr:each>
> >
> > Hope that helps you to understand :-)
>
> 'Fraid not, it blew my head in.  I think I need more paracetamol to
> fight this viral infection -- programming Groovy isn't doing the job :-)
>
> I guess I need to play with Ruby on Rails and Grails from scratch toi
> see how this computational model works currently it just looks like
> overly convoluted mixing of XML and Groovy.  My problem though.
>
> I have a need of creating a small database / finance transaction related
> Web site to try out a business idea.  It took me 3ns to reject PHP, the
> job is not big enough for Java per se so I had decided to use Ruby on
> Rails.  Perhaps I should use Grails?

You could use Grails yes it is getting quite feature rich now...
although it hasnt had an official release yet and has a long way to go
to be as mature as Rails

Graeme

> --
> Russel.
> ====================================================
> Dr Russel Winder                +44 20 7585 2200
> 41 Buckmaster Road              +44 7770 465 077
> London SW11 1EN, UK             [hidden email]
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQBD3j/Nr2EGkixYSboRAhcvAJ9pohknTYcqyHzFSVak0OtDsxSkTgCfRFLi
> TSj9PVzxg2KVBhzNeOWeD0c=
> =E3/4
> -----END PGP SIGNATURE-----
>
>
>