Re: list question
Re: list question
- Subject: Re: list question
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 04 Jun 2003 13:17:56 -0700
On 6/4/03 10:09 AM, "Doug McNutt" <email@hidden> wrote:
>
At 21:11 -0700 6/3/03, Chris Page wrote, and I snipped:
>
> - variables are names for objects
>
> - objects can have multiple names
>
>
> 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"
>
>
>
set a to 2
>
set b to a
>
set c to a + b
>
c
>
--> 4
>
set a to a + 1
>
set c to a + b
>
c
>
--> 5
>
>
Can you explain just how "b" here refers to the same object as "a". Why
>
doesn't the second calculation return 6? (I'd really be upset if it did!)
>
>
Did not "set a to a + 1" mutate the object to which a refers?
No. Please read the posts in this thread. It reassigns the variable 'a' to a
new value. The new value is the sum of the current value of a (2) plus 1.
i.e. 3. The number 2 did not mutate to 3. You can't:
set 2 to 3
Impossible. Therefore
set a to (a + 1)
does not mean 'mutate the value of a (2) to be a + 1'. It's actually using
'a' in two different senses in the same line. 'set a' is reassigning a new
value to the pointer 'a' and not resolving to its current value, whereas the
'a' in '(a + 1)' _is_ resolving it to the value it's currently pointing to,
namely 2. If a hadn't already been set to 2 the line would error as "The
variable a is not defined."
>
Is there an
>
immutable value 2 that still exists after I add the 1? I should hope the
>
memory would be reused.
All integers just exist.
>
>
Does "set" in AppleScript mean the same as "Let" in original BASIC? Or does it
>
work like "set" in M$ VBA where it assigns a local name to, say, a range of
>
cells in an Excel workbook? Both BASIC and VBA use a simple = sign to store a
>
value in memory identified by named variable. A := construct is used
>
elsewhere. AppleScript doesn't allow either but does it replace the (:)= with
>
"set"? Or does it have two separate meanings for "set"?
It can have either meaning: it can point a variable to a mutable object such
as a list or an application object (a range of Excel cells), so the value of
the variable changes as the object changes, or it can point to an immutable
object such as a string or number or an application object that resolves to
a string or number (such as the Value of on single Excel cell) which then
does not change. I'm not positive as to whether the latter implementation in
an application is up to the application developer or not, but I think it
works this way: if the result is given as an application object ('Cell 23 of
Row 7 of Worksheet 1 of Workbook "MyWorksheet.xls') then it's mutable,
whereas is the result is given as a string or number, such as the Value of
that same cell (2.0), then it's not mutable.
There's nothing God-given about Let and Set, or VBA's "=" for assignment to
strings and numbers and "Set =" for application objects. The latter is
required to create a new instance of a VBA Object. In AppleScript, that's
not required, and 'set' can be used for either purpose.
You simply have to learn, and remember, that strings and numbers are
immutable, while lists, records, dates and application objects which have
not been resolved to a string or a number are mutable. It's not that hard to
remember. Just put to one side how other languages may or may not do it, and
learn how AppleScript does it when you're in AppleScript. You seem highly
stressed out that AppleScript doesn't do things just how other languages you
know do it. Well, it doesn't.
>
>
How, in this brave new world of objects does one take a derivative of a
>
variable that is a function of time? Perhaps it is read from a voltmeter with
>
direct memory access. The immutable object mutates by itself.
What are you talking about?
--
Paul Berkowitz
_______________________________________________
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.