Re: *Very* strange script / global variable behavior
Re: *Very* strange script / global variable behavior
- Subject: Re: *Very* strange script / global variable behavior
- From: has <email@hidden>
- Date: Wed, 2 Mar 2005 15:41:31 +0000
Michael Johnston wrote:
This is not a bug, it's a feature! It's a closure! sort of.
While AppleScript does seem to show a slight degree of closure-like
behaviour, e.g.
on foo(x)
script
on f()
set x to x + 1
return x
end f
end script
end foo
set o1 to foo(0)
set o2 to foo(5)
log o1's f() --> 1
log o2's f() --> 6
log o1's f() --> 2
log o2's f() --> 7
this is completely undocumented behaviour and scripters should avoid
it. Especially as there's a perfectly good, reliable and documented
way to achieve the desired result:
on foo(x)
script
property _x : x
on f()
set _x to _x + 1
return _x
end f
end script
end foo
set o1 to foo(0)
set o2 to foo(5)
log o1's f() --> 1
log o2's f() --> 6
log o1's f() --> 2
log o2's f() --> 7
This goes back to what I said in another post about not stressing
AppleScript. Stick to the well-established main roads through the
language and [touch-wood] you'll be fine. Step off the beaten track
and you'll be lost in jungle before you know it. (BTW, the same can
be said for C and most other languages whose complex specs frequently
contain undefined holes.)
"set x to Outer" is instantiating the block Inner.
No it doesn't. It just binds the variable name 'x' to the same script
object as is bound to the variable name 'Outer' and somehow wakes a
slumbering bug in AppleScript's variable management code in the
process. ("Here Be Dragons.")
Closures make implementing systems of callbacks much easier.
Anything you can do with closures you can do with objects; the only
difference is the code for the latter is a bit more verbose. Anyway,
most ASers will never get that far so I shouldn't worry too much
about it. ;)
Cheers,
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden