elukaweski (5) [Avatar] Offline
#1
section 5.5.2 describes a magic variable "owner", which is to be the declaring object. It would seem the the latest version of Groovy (1.5.x) doesn't recognize it. Has it disappeared?
thanks, Edwin
Mittie (397) [Avatar] Offline
#2
Re: magic variable 'owner' in closures
Hi,

no, 'owner' is still there. Can you post the code that seems to suggest otherwise? You can simply try

1.times { println owner }

There are changes between 1.0 and 1.5 in this area, though. "this" now refers to the owner (not the Closure) and there are resolvingStrategies that one can set like "DELEGATE_FIRST", "OWNER_FIRST", and so on...

cheers
Dierk
elukaweski (5) [Avatar] Offline
#3
Re: magic variable 'owner' in closures
Hi:

thanks for the quick response. I just ran script listing 5.8, in the Groovy console, and got the error "groovy.lang.MissingPropertyException: No such Property: owner for class: Mother". When I changed the closure in class Mother from "this.owner" to "owner", it runs as expected.


So, if "this" refers to the owner of the closure, I would assume it would refer to the class Mother. And, I guess, "this.owner", as coded in the closure, refers to the owner of Mother (which is Script?)

thanks
Edwin
Mittie (397) [Avatar] Offline
#4
Re: magic variable 'owner' in closures
> thanks for the quick response. I just ran script
> listing 5.8, in the Groovy console, and got the error
> "groovy.lang.MissingPropertyException: No such
> Property: owner for class: Mother". When I changed
> the closure in class Mother from "this.owner" to
> "owner", it runs as expected.

Yes, you have hit http://groovy.canoo.com/errata/erratum/show/5.
The most annoying erratum in the book that I deeply apologize for. There was a late change in Groovy 1.0 that I still can't understand how it could slip through my self-testing code. I must have tested with the wrong version by accident...

> So, if "this" refers to the owner of the closure, I
> would assume it would refer to the class Mother.

yes.

> And,
> I guess, "this.owner", as coded in the closure,
> refers to the owner of Mother (which is Script?)

No, Mother is not a Closure and therefore has no owner. This is reflected in the error message you get: 'julia.owner' would be access to the 'owner' property of julia, which is not there.

keep groovin'
Dierk
elukaweski (5) [Avatar] Offline
#5
Re: magic variable 'owner' in closures
Thanks, again for the response.....I appreciate that it is an errata. I happened to do a cut and paste from the source that I downloaded....maybe, at least, the download could be changed to the proper code?
Mittie (397) [Avatar] Offline
#6
Re: magic variable 'owner' in closures
good point. We should do that.

Thanks a lot for posting.
Dierk