Re: list question
Re: list question
- Subject: Re: list question
- From: Chris Page <email@hidden>
- Date: Tue, 03 Jun 2003 21:11:32 -0700
On Tuesday, Jun 3, 2003, at 19:18 US/Pacific, Michelle Steiner wrote:
Well, to the scripter and especially to the user, it appears that the
variable a is being changed (mutable means changeable) from "A" to
"AB".
I don't know what to say without sounding argumentative or claiming to
be the arbiter of AppleScript, so I apologize if this comes off wrong,
but I think you're describing variables incorrectly, and I'll try to
explain how I think they're best described:
- variables are names for objects
- objects can have multiple names
- variables can name different objects at different points in time
Furthermore:
- you can change the contents of mutable objects
There is no such thing as "mutating" a variable. All you can do is bind
a variable to a new object. A variable is just a name for some object.
It is not the object itself.
In common english, you could say that you have "changed" a variable
when you change its value. However, this is programming, and there are
more precise terms for what's going on here. In this discussion,
"changing" a variable is called "binding" it to a different object.
"Changing" the contents of an object is called "mutating" the object.
set a to "A" -- a is bound to the immutable string "A"
set b to a -- b is bound to the same immutable string "A"
set c to a & "B" -- c is bound to a new immutable string "AB",
constructed
by copying the contents of "A" and "B"
set a to c -- a is bound to the immutable string "AB"
In this example, there are three strings, "A", "B", and "AB". No
strings are mutated, only created and bound to variables.
The reason it's useful to separate the concepts of binding and mutating
from the single word "change" is that binding only affects a single
variable, but mutations of an object are seen by all the variables
bound to it.
set a to {"A"} -- a is bound to the mutable list {"A"}
set b to a -- b is bound to the same mutable list
set c to a & "B" -- c is bound to a new mutable list {"A", "B"},
constructed
by copying the contents of {"A"} and
concatenating "B"
Note that so far there is no difference between this example, using
mutable lists, and the previous one, using immutable strings. All we're
doing so far is binding variables and creating new objects. Now, let's
mutate a list:
set item 1 of a to "X"
{a, b, c}
--> {{"X"}, {"X"}, {"A", "B"}}
The mutable list {"A"} was changed, and the results can be seen in both
'a' and 'b', because they are both bound to the same mutable list
object. Neither the variable 'a' nor 'b' were "changed" -- they both
still refer to the same object -- rather the object they are bound to
was changed (mutated).
Rather than complicate things by talking about some objects having
"shared storage" -- an implementation detail, really -- I think it's
simpler to just talk about two concepts: variables bound to objects,
and mutable objects.
--
Chris Page - Software Wrangler - Palm, Inc.
_______________________________________________
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.