Re: "a reference to" failure
Re: "a reference to" failure
- Subject: Re: "a reference to" failure
- From: email@hidden
- Date: Thu, 21 Jun 2001 14:41:27 -0400
On Wed, 20 Jun 2001 13:51:58 -0400, "Marc K. Myers" <email@hidden> asked,
>
I wrote the attached script and found that it worked as planned when it
>
was the top level or run handler of a script. I discovered, though,
>
that the size of the applet file grew after running it, so I tried to
>
get rid of the top level variables by putting the body of the script
>
into a lower level handler. When I did this the references to items in
>
the list referred to by "wordRef" stopped working, with the error
>
message "Can't make some data into the expected type". Is there a
>
reason why references like this will only work in top level or run
>
handler code?
I've found that "a reference to" only works for something that has global scope
or global persistence (a property, global, or a "set" variable in the default
run handler, or a script object). It makes sense that if you generate a
reference to a handler's local variable, you would be able to reach into the
handler from outside via that reference. Another way to look at it would be
that "a reference to wordList" where wordList is a local variable of a handler,
would have to mean "a reference to wordList of handler makeGloss", which is
something the language can't do.
Also consider that a local variable isn't persistent from call to call, and if
the handler is recursive, there may be more than one instance of that local
variable. In C, if you tried to use a reference to a local variable, you'd risk
having a pointer to deallocated memory; in AppleScript, it just doesn't let you
try it.
Sure, you know what you mean: "a reference to wordList of this handler". But it
seems the language designers didn't catch that possibility.
The solution is to use something that "a reference to" can be generated from: a
property of the script or a script object, or a global variable, or a variable
defined in the default run handler. (This use is the only good use for a global
that I can see. [Ducks])
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden