Re: script object property contamination
Re: script object property contamination
- Subject: Re: script object property contamination
- From: has <email@hidden>
- Date: Tue, 12 Feb 2002 17:15:42 +0000
Anthony Adachi wrote:
>
As I said before, with regards to the issue I've encountered, the
>
keyword 'my' is not the problem nor is it the solution.
It's half of the solution. The other half is to rework your code so that it
actually does what you want, instead of what you're currently telling it to
do. Let me try to explain...
Here's your latest example doing its thing under static scoping:
======================================================================
script aParent
property FirstParentProp : "Property in parent."
global performTestMethod
to performTestMethod()
end performTestMethod
end script
script Grab_properties_Test
property FirstChildProp : "Property in Chlild"
to get_first_property()
return FirstChildProp
end get_first_property
property parent : aParent
end script
set aParent's performTestMethod to Grab_properties_Test's
[NO-BREAK]get_first_property
tell aParent to performTestMethod()
--> "Property in parent."
======================================================================
Gives a VERY WRONG answer. This is the static scoping bug in action. Bad
bug, bad!
And here's your code using the 'my' keyword to invoke dynamic scoping:
======================================================================
script aParent
property FirstParentProp : "Property in parent."
global performTestMethod
to performTestMethod()
end performTestMethod
end script
script Grab_properties_Test
property FirstChildProp : "Property in Chlild"
to get_first_property()
return my FirstChildProp
end get_first_property
property parent : aParent
end script
set aParent's performTestMethod to Grab_properties_Test's
[NO-BREAK]get_first_property
tell aParent to performTestMethod()
--> Error: "Can't get FirstChildProp."
======================================================================
Here AS is behaving absolutely correctly - based on what you're telling it
to do. It just so happens that what you _think_ you're telling it and what
you're _actually_ telling it are not the same thing.:)
I think you've misunderstood how inheritance works in AS. Y'see, children
can 'see' the contents of their parents, but parents CANNOT 'see' the
contents of their children. So if we hide away the stuff that can't be
'seen' by aParent, what's actually happening is the following:
======================================================================
script aParent
property FirstParentProp : "Property in parent."
to performTestMethod()
return my FirstChildProp
end performTestMethod
end script
tell aParent to performTestMethod()
--> Error: "Can't get FirstChildProp."
======================================================================
So as I said last time, it's all a matter of whose context you're in (i.e.
where you're currently 'standing'), and that all depends on which script
object you point that 'tell' block at.
Since you're talking to aParent here, performTestMethod is looking for
FirstChildProp within aParent. And since it can't find it there it quite
rightly says: "Can't get FirstChildProp." (Though you seem to be getting a
weird-looking error message instead - I don't know why.)
Now try the following:
======================================================================
script aParent
property FirstParentProp : "Property in parent."
global performTestMethod
to performTestMethod()
end performTestMethod
end script
script Grab_properties_Test
property FirstChildProp : "Property in Chlild"
to get_first_property()
return my FirstChildProp
end get_first_property
property parent : aParent
end script
set aParent's performTestMethod to Grab_properties_Test's
[NO-BREAK]get_first_property
tell Grab_properties_Test to performTestMethod()
--> "Property in Chlild"
======================================================================
This time you're talking to the child script, so that's where
performTestMethod starts looking for FirstChildProp.
Therefore:
1. Use the 'my' keyword to invoke dynamic scoping as necessary and avoid
that damned static scoping bug
2. Redesign your code so it can actually do what you intend it to do; i.e.
watch whose context you're in.
BTW, this inheritance stuff's a hundred times easier to understand with the
benefit of a Nice Clear Diagram, and if anyone is able to point to any
instances of this all too rare species then please do [1]. Meantime, I hope
the above explanation is clear enough.
Cheers
has
[1] Alternatively, throw generous cash sums at me and happily I'll do 'em
for you.;)
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.