Dates gone wild
Dates gone wild
- Subject: Dates gone wild
- From: Paul Berkowitz <email@hidden>
- Date: 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"}
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"}
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
In ex. 1, the variable d is reassigned to another date value, but that
doesn't affect the value of the list ls. The same thing happens if you
assign the value "a" or 3. The list ls does not alter.
In ex. 2, d is changed dynamically by 'change month of d'. Since lists are
also dynamic, the value of d inside ls also changes, so teh value of teh
list changes too: it's like 'set item 1 of ls to aNewValue'.
But what's going on in ex. 3? Here d is being reassigned, just as in ex. 1,
only it's doing so as the result of a handler. If the handler merely
reassigns a new value, (set aDate to date "12/5/03") that does not affect
ls. But changing the month of aDate changes ls back home! It's as if it has
global scope.
It makes no difference throwing 'local' around:
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
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
I find ex. 3 and 4 very strange indeed.
--
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.