|
Hi,
I'm looking for a good definition of this vs delegate vs owner. I think I understand -- 'this' refers to the enclosing object where the closure is defined; 'delegate' refers to the enclosing object when the closure is executed, right? Then does 'owner' refer to the method scope if the closure is defined w/in a method? The wiki documentation has this example on the Closures page: class HiddenMember { private String name; getClosure (String name) { return { name -> println ("Argument: ${name}, Object: ${owner.name}")} } } Which for me doesn't compile, because it's missing the "def" before the method definition. Adding "def" in, I get this: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script6: 6: The current scope does already contain a variable of the name name @ line 6, column 12. (Under the Groovy 1.1 snapshot included w/ Grails SVN HEAD) I scanned GINA briefly and didn't find a great explanation either (maybe I missed it). So if someone can clarify for me, I'd be happy to update the wiki documentation. Thanks. -Tom --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
Tom Nichols schrieb:
> Hi, > > I'm looking for a good definition of this vs delegate vs owner. I > think I understand -- > > 'this' refers to the enclosing object where the closure is defined; > 'delegate' refers to the enclosing object when the closure is executed, right? > > Then does 'owner' refer to the method scope if the closure is defined > w/in a method? I would say it different... this -> refers to the enclosing class instance owner -> refers to the enclosing object (a closure or this) delegate -> by default the same as owner, but changeable for example in a builder > The wiki documentation has this example on the Closures page: > class HiddenMember { > private String name; > > getClosure (String name) > { > return { name -> println ("Argument: ${name}, Object: ${owner.name}")} > } > } > > Which for me doesn't compile, because it's missing the "def" before > the method definition. can you please post the link? the example is extremely out of date. > Adding "def" in, I get this: > > org.codehaus.groovy.control.MultipleCompilationErrorsException: > startup failed, Script6: 6: The current scope does already contain a > variable of the name name > @ line 6, column 12. > > (Under the Groovy 1.1 snapshot included w/ Grails SVN HEAD) yes, the closure defines another variable name... I guess the example is from before beta-9! that's very old. bye blackdrag -- Jochen "blackdrag" Theodorou Groovy Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
It's the main "Closures" page on the user guide:
http://groovy.codehaus.org/Closures Do a page search for "owner," it's the code example directly below the first occurrence. If you happen to update that page, I'd suggest refactoring that wiki page into a number of smaller pages. That page has _a_lot_ of content and it's a bit unwieldy to scroll through. It's on my list to work on if nobody else gets around to it. Thanks! -Tom On 9/7/07, Jochen Theodorou <[hidden email]> wrote: > Tom Nichols schrieb: > > Hi, > > > > I'm looking for a good definition of this vs delegate vs owner. I > > think I understand -- > > > > 'this' refers to the enclosing object where the closure is defined; > > 'delegate' refers to the enclosing object when the closure is executed, right? > > > > Then does 'owner' refer to the method scope if the closure is defined > > w/in a method? > > I would say it different... > > this -> refers to the enclosing class instance > owner -> refers to the enclosing object (a closure or this) > delegate -> by default the same as owner, but changeable for example in > a builder > > > The wiki documentation has this example on the Closures page: > > class HiddenMember { > > private String name; > > > > getClosure (String name) > > { > > return { name -> println ("Argument: ${name}, Object: ${owner.name}")} > > } > > } > > > > Which for me doesn't compile, because it's missing the "def" before > > the method definition. > > can you please post the link? the example is extremely out of date. > > > Adding "def" in, I get this: > > > > org.codehaus.groovy.control.MultipleCompilationErrorsException: > > startup failed, Script6: 6: The current scope does already contain a > > variable of the name name > > @ line 6, column 12. > > > > (Under the Groovy 1.1 snapshot included w/ Grails SVN HEAD) > > yes, the closure defines another variable name... I guess the example is > from before beta-9! that's very old. > > bye blackdrag > > -- > Jochen "blackdrag" Theodorou > Groovy Tech Lead (http://groovy.codehaus.org) > http://blackdragsview.blogspot.com/ > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Jochen Theodorou
On 9/7/07, Jochen Theodorou <[hidden email]> wrote:
> Tom Nichols schrieb: > > Hi, > > > > I'm looking for a good definition of this vs delegate vs owner. I > > think I understand -- > > > > 'this' refers to the enclosing object where the closure is defined; > > 'delegate' refers to the enclosing object when the closure is executed, right? > > > > Then does 'owner' refer to the method scope if the closure is defined > > w/in a method? > > I would say it different... > > this -> refers to the enclosing class instance > owner -> refers to the enclosing object (a closure or this) > delegate -> by default the same as owner, but changeable for example in > a builder > Can you please exemplify when owner is different from this, and give a pointer for changing the delegate usage. tia, ./alex -- .w( the_mindstorm )p. --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Thom Nichols
Tom Nichols schrieb:
> It's the main "Closures" page on the user guide: > http://groovy.codehaus.org/Closures > > Do a page search for "owner," it's the code example directly below the > first occurrence. thank you very much, I added the def. > If you happen to update that page, I'd suggest refactoring that wiki > page into a number of smaller pages. That page has _a_lot_ of content > and it's a bit unwieldy to scroll through. while I am not into one page web sites I agree with you that the page is a bit too big... > It's on my list to work on if nobody else gets around to it. We encourage our users to modify the documentation. Because as a creator you see some things different, you read the page without reading it and then you see no big content at all ;) bye blackdrag -- Jochen "blackdrag" Theodorou Groovy Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Alex Popescu
Alexandru Popescu ☀ schrieb:
[...] > Can you please exemplify when owner is different from this, and give a > pointer for changing the delegate usage. 1) owner/this class X { def myMethod() { def c0 = { def c1 = {} } co } } values (not legal code!) c0.this = instance of X c0.owner = c0.this c1.this = instance of X c1.owner = c0 while "this" will always be a reference to the class X here, owner refers to the enclosing closure or class, whatever comes first. 2) delegate: def c = { foo() } try { c() assert false, "should have thrown an exception" } catch (MissingMethodException mme) { assert true } class X{ def foo(){1} } c.delegate = new X() assert c()==1 Usually the delegate is changed in a builder, but that is not enforced. bye blackdrag -- Jochen "blackdrag" Theodorou Groovy Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Thom Nichols
Hi Graeme,
So when an ExpandoMetaClass is extended with a closure, it is setting the delegate to the instance on which the method is called? I didn't notice this mentioned on the wiki documentation, but it is a very handy thing to note since you can't use "this" as you would if you were writing a method definition. Please correct me if it is documented somewhere. i.e. class Book { String title } Book.metaClass.printTitle << { //println owner.title //println this.title println delegate.title } new Book( title:"GINA" ).printTitle() Thanks. -Tom On 9/7/07, Tom Nichols <[hidden email]> wrote: > Hi, > > I'm looking for a good definition of this vs delegate vs owner. I > think I understand -- > > 'this' refers to the enclosing object where the closure is defined; > 'delegate' refers to the enclosing object when the closure is executed, right? > > Then does 'owner' refer to the method scope if the closure is defined > w/in a method? > > The wiki documentation has this example on the Closures page: > class HiddenMember { > private String name; > > getClosure (String name) > { > return { name -> println ("Argument: ${name}, Object: ${owner.name}")} > } > } > > Which for me doesn't compile, because it's missing the "def" before > the method definition. Adding "def" in, I get this: > > org.codehaus.groovy.control.MultipleCompilationErrorsException: > startup failed, Script6: 6: The current scope does already contain a > variable of the name name > @ line 6, column 12. > > (Under the Groovy 1.1 snapshot included w/ Grails SVN HEAD) > > I scanned GINA briefly and didn't find a great explanation either > (maybe I missed it). So if someone can clarify for me, I'd be happy > to update the wiki documentation. > > Thanks. > -Tom > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | See how NAML generates this page |
