Closures and Context

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

Closures and Context

Bernd Schiffer
Hi.

Should that be possible?

class Animal {
    def sound(organ) { organ() }
}

class Cat extends Animal {
    def word = 'miau'
}

class Dog extends Animal {
    def word = 'wau'
}

def organ = { word }

assert 'miau' == new Cat().sound(organ)
assert 'wau' == new Dog().sound(organ)

Groovy complains about not finding "word" in Animal.sound(). But I thought it should do so, because closure are evaluated at runtime...?! What am I missing here?

Kind regards,
   Bernd

--
Blog: http://berndschiffer.blogspot.com



Heute schon einen Blick in die Zukunft von E-Mails wagen? Versuchen Sie´s mit dem neuen Yahoo! Mail.
Reply | Threaded
Open this post in threaded view
|

Re: Closures and Context

Siegfried Puchbauer
What about

def organ = { this.word }

Cheers

On 9/18/07, Bernd Schiffer <[hidden email]> wrote:
Hi.

Should that be possible?

class Animal {
    def sound(organ) { organ() }
}

class Cat extends Animal {
    def word = 'miau'
}

class Dog extends Animal {
    def word = 'wau'
}

def organ = { word }

assert 'miau' == new Cat().sound(organ)
assert 'wau' == new Dog().sound(organ)

Groovy complains about not finding "word" in Animal.sound(). But I thought it should do so, because closure are evaluated at runtime...?! What am I missing here?

Kind regards,
   Bernd

--
Blog: <a href="http://berndschiffer.blogspot.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://berndschiffer.blogspot.com



Heute schon einen Blick in die Zukunft von E-Mails wagen? Versuchen Sie´s mit dem <a href="http://de.rd.yahoo.com/evt=40591/*http://de.docs.yahoo.com/ymail/landing.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> neuen Yahoo! Mail.



--
_______________________
Siegfried Puchbauer
http://siegfried.puchbauer.com/
Reply | Threaded
Open this post in threaded view
|

Re: Closures and Context

Guillaume Laforge
Administrator
Or you may have to set the delegate of the closure before calling it.
But then doing so, in case of multithreaded environment, you should
probably clone the closure before calling it.

On 9/18/07, Siegfried Puchbauer <[hidden email]> wrote:

> What about
>
> def organ = { this.word }
>
> Cheers
>
>
> On 9/18/07, Bernd Schiffer <[hidden email] > wrote:
> >
> >
> > Hi.
> >
> > Should that be possible?
> >
> > class Animal {
> >     def sound(organ) { organ() }
> > }
> >
> > class Cat extends Animal {
> >     def word = 'miau'
> > }
> >
> > class Dog extends Animal {
> >     def word = 'wau'
> > }
> >
> > def organ = { word }
> >
> > assert 'miau' == new Cat().sound(organ)
> > assert 'wau' == new Dog().sound(organ)
> >
> >
> > Groovy complains about not finding "word" in Animal.sound(). But I thought
> it should do so, because closure are evaluated at runtime...?! What am I
> missing here?
> >
> > Kind regards,
> >    Bernd
> >
> > --
> > Blog: http://berndschiffer.blogspot.com
> >
> >
> > ________________________________
> Heute schon einen Blick in die Zukunft von E-Mails wagen? Versuchen Sie´s
> mit dem neuen Yahoo! Mail.
>
>
>
> --
> _______________________
> Siegfried Puchbauer
> http://siegfried.puchbauer.com/


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy

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

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Closures and Context

paulk_asert
In reply to this post by Bernd Schiffer

I would probably write this example like:

class Animal {
    def word
    def organ = { word }
    def sound() { organ() }
}

class Cat extends Animal {
    Cat(){ word = 'miau'; organ = { word * 2 } }
}

class Dog extends Animal {
    Dog(){ word = 'wau' }
}

assert 'miaumiau' == new Cat().sound()
assert 'wau' == new Dog().sound()


Bernd Schiffer wrote:

> Hi.
>
> Should that be possible?
>
> class Animal {
>     def sound(organ) { organ() }
> }
>
> class Cat extends Animal {
>     def word = 'miau'
> }
>
> class Dog extends Animal {
>     def word = 'wau'
> }
>
> def organ = { word }
>
> assert 'miau' == new Cat().sound(organ)
> assert 'wau' == new Dog().sound(organ)
>
> Groovy complains about not finding "word" in Animal.sound(). But I
> thought it should do so, because closure are evaluated at runtime...?!
> What am I missing here?
>
> Kind regards,
>    Bernd
>
> --
> Blog: http://berndschiffer.blogspot.com
>
>
> ------------------------------------------------------------------------
> Heute schon einen Blick in die Zukunft von E-Mails wagen? Versuchen
> Sie´s mit dem neuen Yahoo! Mail
> <http://de.rd.yahoo.com/evt=40591/*http://de.docs.yahoo.com/ymail/landing.html>.
>


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

    http://xircles.codehaus.org/manage_email