Re: copy & set statements
Re: copy & set statements
- Subject: Re: copy & set statements
- From: has <email@hidden>
- Date: Wed, 24 Oct 2001 15:33:30 +0100
Timothy Bates wrote:
>
This is getting weird:
>
>
set theCat to {"woody", "hungry"}
>
myHandler(theCat)
>
theCat --> {woody,hungry}
>
>
on myHandler(myList)
>
set myList to myList & "a" -- make a change within the list
>
end myHandler
>
>
BUT
>
>
>
set theCat to {"woody", "hungry"}
>
myHandler(theCat)
>
theCat -->{"woody", "hungry", "a"}
>
>
on myHandler(myList)
>
set end of myList to "a" -- make a change within the list
>
end myHandler
>
>
So, this "feature" only bites when you don't cause AS to dereference (and
>
copy) the pointer to the passed in parameter, hence Nigel's admonition to
>
explicity copy it if you can't afford external effects, but not to rely on
>
the external effect (calling a global instead to insure this) because it
>
only happens if you don't accidentally cause AS to transparently coerce the
>
parameter.
>
>
No wonder my scripts had worked in the past - sheer dumb luck ;-)
I can't speak for anyone else, but I'd say this is what keeps me afloat for
much of the time.:)
In my case, I was sorting and filtering a list of lists as it was being
passed through various handlers, only to find later that the original
'master' list was being messed up as a result. In fact, even using "copy x
to y" didn't work (references were still getting through somewhere),
although "foo(every item of x)" did. (Note that I eventually stumbled on
this hack fix after trying pretty much everything else. But isn't that
always the way?) So if you think it's bad already with lists, try it with
lists of lists - and so on... :p
If I was doing it over again (given what I know now), I'd pay much more
attention to what was going where and what might happen on the way. I'd
also try to write everything to use/pass only lists of references (pointing
back to the master list) so that I could reduce the amount of data copying
going on as much as possible (the original script was dog-slow, no doubt
partly due to all the copying going on).
OTOH, I feel this is rather more work than I'd been expecting when I first
got into AS. :/
has