Re: list question
Re: list question
- Subject: Re: list question
- From: Chris Page <email@hidden>
- Date: Wed, 4 Jun 2003 18:17:55 -0700
On Wednesday, Jun 4, 2003, at 10:09 US/Pacific, Doug McNutt wrote:
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!)
At the end of your example, "b" does not refer to the same object as
"a". "set a to a + 1" binds "a" to the result of "a + 1". Let me break
this down:
set a to 2 -- bind a to immutable integer 2
--> 2
set b to a -- bind b to the same integer
--> 2
set c to a + b -- calculate the sum of a and b,
which is the immutable integer 4,
then bind the result to c
--> 4
set a to a + 1 -- calculate the sum of a and 1,
which is the immutable integer 3,
then bind the result to a
--> 3
set c to a + b -- calculate the sum of a and b,
which is immutable integer 5,
then bind the result to c
--> 5
Is there an immutable value 2 that still exists after I add the 1? I
should hope the memory would be reused.
In fact, when it comes to immutable objects, whether or not there is
more than one instance of that object is an implementation detail,
because there's no way for you to tell from within AppleScript.
As it happens, it is almost entirely certain that there are multiple
copies of each number. When you bind multiple variables to the same
number, the number is most likely copied in memory to make it
immediately accessible, rather than having to indirectly access the
single instance of that number. Numbers generally fit in a small enough
amount of memory that making copies actually takes less memory than
having one instance and pointing to it.
As for strings, it's possible that each instance of a string is a
unique copy, but there is also a possibility that AppleScript keeps
track of each unique string and reuses them instead of making copies.
This saves memory, but can be slower, because you have to look to see
if the string you want already exists. Also, it could be that strings
with only one character are copied around like numbers.
In any case, since AppleScript uses a garbage collector, if immutable
objects are copied the memory will be recovered when they are no longer
used.
And, again, these are all implementation details not visible from
within the language, so in terms of writing correct scripts, you don't
care.
As a practical matter, of course, you do care about memory consumption,
especially on Mac OS 9, where there is a fixed memory partition size
for applications. But that is something for you, the script writer, to
determine by testing your script, not by any means within AppleScript.
--
Chris Page - Software Wrangler - Palm, Inc.
Go to MacHack...or die trying: <
http://www.machack.com/>
_______________________________________________
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.