Re: copy & set statements
Re: copy & set statements
- Subject: Re: copy & set statements
- From: Chris Page <email@hidden>
- Date: Wed, 24 Oct 2001 03:24:30 -0700
[Warning: I'm on a role...]
[Warning 2: I'm not an AppleScript expert, but I'm pretty sure my
understanding of the relationship between the following concepts and
AppleScript are essentially correct. I'm sure someone will let me know if I
got something wrong.]
nigh on 2001.10.23 4:11 PM, email@hidden at email@hidden wrote:
>
Actually, everything is passed as a pointer, but because AppleScript coerces
>
almost everything invisibly, the pointerness of simple values isn't apparent.
Probably a better way to look at it is that you are passing objects around.
Numbers are objects, lists are objects, records are objects. Some objects
are "mutable", meaning that their values can be altered, and some objects
are not (i.e. they are "immutable"). Lists, strings, and records are mutable
-- you can add, replace, and remove objects they contain. Numbers,
enumerations, and booleans are immutable -- you can't alter the value of the
number 5.
It's important to note that for all intents and purposes there is only one
instance of the number 5, for example. When you pass it around, from handler
to handler, or when you assign it to a variable, you are not copying it.
You're passing around the one and only instance, and it's immutable, so its
value will never change. It's passed around and assigned exactly the same
way as when you pass around a list or a record (they just happen to be
mutable, so their values can change).[*]
There is no reason to bother thinking in terms of "passed by value" (copied)
or "passed by reference" (pointed at). Those are lower level concepts and
they aren't used in AppleScript. They apply to the arguments to a
function/handler. In a language like Pascal or C, a given function
determines whether and which of its arguments are passed by value or by
reference. In AppleScript, all handler arguments are passed around in the
same way: "by object".
Now, an AS "reference" is not really the same thing as "passed by reference"
in other languages. In fact, it's something more powerful. An AS reference
is an object that describes how to get another object, which may or may not
exist, and exactly which object you get from it may change through time. At
one point in time, a particular reference may refer to the number 5, and at
another time, it may refer to a record. Once you "get" the object that the
reference describes, you can continue work with that particular object,
regardless of whether the reference continues to refer to it. If the object
is mutable, its value may change, but you're still working with the same
object.
So, an AS reference is another kind of object, just like the number 5 or the
list {one, 2, "three"}, and it is passed "by object". When you pass a
reference object to a handler, the handler receives the reference object.
When some AS code wants to work with the object that the reference object
describes, it uses "get" to get it. Until "get" is used to get an object,
you're still just working with the reference object.
Now this is where my AS knowledge trails off, so I can't give examples, but
it's my understanding that some AS operators implicitly perform a "get" when
working with a reference object. This can be a source of confusion, but you
just have to keep in mind that a reference is an object that is passed
around just like all other objects, that "get" must be invoked to get the
object it describes, and that "get" can be invoked implicitly by particular
handlers or operators. [I need to educate myself on when "get" is implicitly
invoked.]
[*] Technical footnote (don't let this confuse you about the above, ignore
it if it doesn't make sense to you): In a typical OO language
implementation, most objects are represented by a memory address (a
"pointer") stored in a register or a location in memory. For efficiency,
common objects, like small integers, true, and false, are represented
directly instead of using a memory address, and a flag is stored along with
the value to indicate how the object is represented. Some people might refer
to these as "by reference" and "by value", but that would be confusing the
issue -- instead, the terms "un-boxed" and "boxed", respectively, are often
used (i.e. the object is stored directly "in the box" or in some memory
location "outside of the box"). At this low level, you might say the number
5 was being copied when it gets passed around, but that's just an
implementation detail -- there's still only one object for the number 5 as
far as AS can tell -- we're just representing the object using a shortcut
instead of having to allocate some other memory to store it.
--
Chris Page
Mac OS Lead, Palm Desktop
Palm, Inc.
One of the symptoms of an approaching nervous breakdown is the belief that
one9s work is terribly important. - Bertrand Russell