Re: Dates gone wild
Re: Dates gone wild
- Subject: Re: Dates gone wild
- From: Axel Luttgens <email@hidden>
- Date: Tue, 09 Sep 2003 17:19:23 +0200
Doug McNutt wrote:
At 10:10 +0100 9/9/03, Nigel Garvey wrote:
Paul Berkowitz wrote on Mon, 08 Sep 2003 21:05:38 -0700:
set ls to {}
set d to date "Friday, September 5, 2003 12:00:00 AM"
set end of ls to d
set d to date "Sunday, October 5, 2003 12:00:00 AM"
ls
--> {date "Friday, September 5, 2003 12:00:00 AM"}
That's right. The variable d isn't in the list, it just points to the
same date as is pointed to by the one pointer in the list. When you set d
to a different date, it simply points to a different date object. The
pointer in the list still points to the original.
Not long ago I was chastised for using the term "pointer" on this list. I was trying to figure out what a "reference" really was.
I remember having read that thread, and hesitated to intervene as so
much had already been written.
But, as you express hereafter some kind of frustration, please allow me
to bring my small contribution.
First of all, one should be aware that Paul's writing was about "data
sharing" in AppleScript's sense.
Just as a reminder:
-- Create an anonymous object (a list) and bind it to identifier x
set x to {1, 2, 3}
-- Bind identifier y to the list currently bound to x
set y to x
-- Act upon that list through identifier x
set item 1 of x to 100
-- Get that list through any of x and y
x
--> {100,2,3}
y
--> {100,2,3}
-- Act upon that list through identifier y
set item 2 of y to 200
x
-->{100,200,3}
y
-->{100,200,3}
Note that this works with keyword 'set'.
If you use 'copy' instead, data sharing won't occur:
-- Create a list and bind it to identifier x
set x to {1, 2, 3}
-- Make a copy of the list bound to x and bind that copy to y
copy x to y
-- Act upon the list bound to x
set item 1 of x to 100
-- Get lists bound to x and y
x
--> {100,2,3}
y
--> {1,2,3}
The same way, data sharing won't occur if the object is a "small" one:
set x to 1
set y to x
set x to 100
x
--> 100
y
--> 1
And yes, I guess one could really speak about pointers in the case of
data sharing.
That is, by writing 'set x to 1', one must be playing with some kind of
structure having at least placeholders for a label ('x'), a type
identifier ('integer') and a value ('1').
On the other hand, with 'set x to {1,2,3}', those placeholders could be
label ('x'), type ('list') and value (pointer to another structure
representing the list).
Now, unless I'm wrong, data sharing has been introduced to mainly
preserve memory in those times of scarce memory.
I mean that, if the dynamic structure of lists requires some kind of
dynamic memory handling, data sharing itself is not technicaly nor
logicaly required.
In pure AppleScript, data sharing (ie that particular given affected to
the keyword 'set') occurs with lists, records, dates (very similar to
records), and script objects.
Are you saying that "set end of ls to d" really means "place a reference to d at the end of ls"?
I don't think Nigel was speaking about references in that case.
This is still data sharing, even if you have to use a more complicated
reference form than just naming a variable:
set d to {1, 2, 3}
set x to {}
set end of x to d
set item 1 of d to 100
d
--> {100,2,3}
x
--> {{100,2,3}}
set item 2 of item 1 of x to 200
d
-->{100,200,3}
x
-->{{100,200,3}}
If so I would expect changing the value stored in the reference would change the result obtained above.
A you have noticed it, there is no change.
Just because this is data sharing:
-- Create list {1,2,3} and bind it to identifier d
set d to {1, 2, 3}
-- Evaluate {d}, which creates a list whose single item is
-- the list created above, and bind it to identifier x
set x to {d}
x
--> {{1,2,3}}
-- Create list {100,200,300} and bind it to identifier d
-- Note that this destroys the binding of identifier d with
-- initial list {1,2,3} but not that list istself.
set d to {100, 200, 300}
x
--> {{1,2,3}}
A a "proof", consider this variant:
-- Variant 1
set d to {1, 2, 3}
set e to d
set x to {d}
-- This yields the same result as above
set d to {100, 200, 300}
x
--> {{1,2,3}}
-- But identifier e is still bound to initial list {1,2,3}, so...
set item 1 of e to 100
x
--> {{100,2,3}}
Does it matter if d is an object or a variable?
As far as the semantics of 'set' is concerned, yes (see above).
But your usage of 'object' and 'variable' is somewhat confusing.
I'll go over references in a subsequent post, as this one becomes
somewhat lengthy...
But just consider this second variant:
-- Variant 2
set d to {1, 2, 3}
set x to {a reference to d}
x
--> {d of +script;}
contents of x
--> {{1, 2, 3}}
-- No data sharing here, as wet set d to a simple value
set d to 1
-- But x still references d, and thus the value bound to d
x
--> {d of +script;}
contents of x
--> {1}
HTH,
Axel
_______________________________________________
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.