Re: Dates gone wild
Re: Dates gone wild
- Subject: Re: Dates gone wild
- From: Nigel Garvey <email@hidden>
- Date: Tue, 9 Sep 2003 10:10:51 +0100
Paul Berkowitz wrote on Mon, 08 Sep 2003 21:05:38 -0700:
>
I'm aware that both lists and dates are mutable (dynamic), but there's
>
something very weird about the following:
>
>
1.
>
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.
>
2.
>
set ls to {}
>
set d to date "Friday, September 5, 2003 12:00:00 AM"
>
set end of ls to d
>
set month of d to October
>
ls
>
--> {date "Sunday, October 5, 2003 12:00:00 AM"}
Here you're changing internal details of the date object itself. Both d
and the item in the list are still pointing to the same object and so
both see the internal change.
>
3.
>
set ls to {}
>
set d to date "Friday, September 5, 2003 12:00:00 AM"
>
set end of ls to d
>
set d to AddAMonth(d)
>
ls
>
--> {date "Sunday, October 5, 2003 12:00:00 AM"} -- !!!
>
>
to AddAMonth(aDate)
>
set month of aDate to October
>
return aDate
>
end AddAMonth
Here d, the first pointer in ls, and aDate all point to the same date
object. When you return aDate and set d to the result, you're actually
setting d to itself. You'd get the same final value for ls if you'd
called AddAMonth(d) without setting d to any result.
>
4.
>
local d
>
>
set ls to {}
>
set d to date "Friday, September 5, 2003 12:00:00 AM"
>
set end of ls to d
>
set d to AddAMonth(d)
>
ls
>
--> {date "Sunday, October 5, 2003 12:00:00 AM"}
>
>
to AddAMonth(aDate)
>
local f
>
>
set f to aDate
>
set month of f to October
>
return f
>
end AddAMonth
Item 1 of ls, d, aDate, and f are all pointers to the same date object.
>
The only thing that works is to copy aDate to f, rather than set f to aDate:
>
>
5.
>
set ls to {}
>
set d to date "Friday, September 5, 2003 12:00:00 AM"
>
set end of ls to d
>
set d to AddAMonth(d)
>
ls
>
--> {date "Friday, September 5, 2003 12:00:00 AM"}
>
>
to AddAMonth(aDate)
>
copy aDate to f
>
set month of f to October
>
return f
>
end AddAMonth
Or:
to AddAMonth(aDate)
copy aDate to aDate -- set aDate to a duplicate of the original object
set month of aDate to October
return aDate
end AddAMonth
>
I find ex. 3 and 4 very strange indeed.
It's the difference between variables and the data they "contain" - or,
in fact, "to which they point." (Variables are pointers to data.) If you
'set' one variable to another, you simply duplicate the pointer. You
don't duplicate the data and you don't "put one variable inside the
other." Similarly, when you set the end of a list to a variable, you copy
the pointer to the list. The variable and the list item then point to the
same object, but are separate entities themselves. You can set either of
them to something else without affecting the other; but if you change
some aspect of the object to which they both point, they'll both see the
change in that object. Here, we're talking about a date object. When you
change its month (and consequently its weekday), it's still the same date
object but with some internal changes.
When you pass a parameter to a handler, it's the pointer that gets
passed, not the value to which it points. aDate points to the same date
object as is pointed to by d and by the item in ls. In your example 4,
you're also setting f to point to that same object, so anything you do to
that object through f will also show up when you access it via the other
three means. The variables f and aDate are both local *variables*, but
the object to which they point has no such restrictions.
The 'copy' command duplicates the *data* (entire structures, if
necessary) and sets up pointers to the duplicates. 'copy aDate to aDate'
creates a duplicate of the original date object and resets aDate to point
to the duplicate. Obviously, you could arrange for d and the list item to
be different like this:
2a.
set ls to {}
set d to date "Friday, September 5, 2003 12:00:00 AM"
copy d to end of ls -- 'copy', not 'set'
set d's month to October
ls
--> {date "Friday, September 5, 2003 12:00:00 AM"}
Apologies for verbosity. ;-)
NG
_______________________________________________
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.