Re: what is the best way to 'unset' a variable
Re: what is the best way to 'unset' a variable
- Subject: Re: what is the best way to 'unset' a variable
- From: has <email@hidden>
- Date: Sat, 23 Mar 2002 13:33:42 +0000
Steven Angier wrote:
>
You can destroy a variable in AppleScript by setting its value to nothing,
>
though I'm not
>
sure why you'd want to:
>
>
set x to 1
>
set x to GetNothing()
>
return x
>
>
on GetNothing()
>
return
>
end
Uh-uh. That just gives an error on the second line: "No result was returned
from some part of this expression."
Anyway, you can't "destroy a variable" - a variable is simply a pointer to
an object (a block of data stored in memory) [1]. Lots of folks tend to
confuse the two, but it's worth being clear on the difference - especially
once you get into dealing with complex data structures like lists, records,
references and script objects where this sort of stuff really starts to
matter.
As for objects, they're destroyed automatically by AppleScript once they're
no longer in use anywhere. That frees up memory for future use. (Some
languages, e.g. C, leave this sort of dull, laborious cleaning-up work
entirely to the programmer to deal with, but here in AppleScript things are
a bit more civilised.:)
If you've got aglobal variable in your script that you've stuffed a 5MB
string into, you may want to flush that big ol' thing out of there before
your script finishes to avoid leaving a heinously large script file/applet
on your hard drive. Which is easiest done by using something like:
set variableName to missing value
Or 0/""/{}/etc - whatever takes your fancy. (I'm not sure about 'null'
though - is that a standard AS keyword or 3rd-party?) Assuming no other
properties/globals are pointing to the same object then that object is
promptly disposed of.
(Whether or not you should be using a property/global variable in the first
place is another question, however.:)
As for local variables, those are all destroyed when the handler they're
declared in returns (whether it's a standard run or open handler, or one
defined by the script author). So you never need to worry about those.
HTH
has
[1] If you prefer to think metaphorically, a variable is effectively a
container into which you put an object. (Except this metaphor might also
confuse a little as you can have many variables 'containing' the same
object.)
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.