Re: set vs copy?
Re: set vs copy?
- Subject: Re: set vs copy?
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 15 Sep 2010 09:32:57 -0400
On Wed, Sep 15, 2010 at 4:38 AM, Thomas Fischer <email@hidden> wrote:
> Yes, my only question is how do you know which is which?
> That
> set A to A & 4
> and
> set end of A to 4
> are different things seems to me to be an implementation issue
They're not different; in each case, "set" is doing exactly the same
thing. You're just aiming it at different targets.
Try this:
set A to {1,2,3}
set B to {}
set end of B to A
set end of A to 4
log(B)
You get 1,2,3,4. If you instead do this:
set A to {1,2,3}
set B to {}
copy A to end of B
set end of A to 4
log(B)
You get 1,2,3. Again, with set, you wind up with two things (the
variable A and the last element of the list pointed to by the variable
B) pointing to the same list value. With copy, you get two distinct
list values.
So set is consistent in its behavior, whether the thing being set is
"A" or "end of B" or something else. But it's important to understand
exactly what that behavior is:
Despite appearances, set does not modify values. It doesn't know how.
All it can do is replace values with other values. It's the
difference between editing a text file and replacing it by dropping a
new file with the same name in the folder.
The distinction is sometimes subtle, and may seem nit-picky, but it's
at the heart of this discussion. It's easiest to see with simple
values, like numbers:
set A to 4
set B to A
set A to A + 1
the last line doesn't modify the number 4, which is still sitting
there unmolested in B (and elsewhere). Instead, it makes A contain a
different number (5). The numbers themselves are unchangeable -
"immutable" in CS-speak.
But when you have values that are aggregates, like lists or records,
and are allowed to perform this sort of replacement on the nested
values inside them, then you suddenly have a different situation.
Replacing the value contained by an element of a list may not modify
that element, but it does modify the list! Inner replacement equates
to outer modification.
In some languages, this sort of modification is just as impossible as
changing a number - if you try to modify a list, you just get a brand
new list with the modification applied, leaving the original
untouched. But AppleScript doesn't work that way. Lists and records
are mutable.
So the real key to the difference between "set A" and "set end of B"
is that "of". As soon as you're looking inside a structured value of
some sort, set gains the capability of modifying that structure in
place. And that's when the difference between set and copy matters.
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden