Re: POSIX and lists question
Re: POSIX and lists question
- Subject: Re: POSIX and lists question
- From: "Mark J. Reed" <email@hidden>
- Date: Sat, 13 Aug 2005 00:58:38 -0400
OK, just finally sat down and looked at has's counter-closure examples...
script a
property x : 2
on foo()
return x
end foo
end script
script b
property z : 0
property foo : a's foo
end script
b's foo() --> 0
That's the most straightforward of your examples, but it's still a
little weird. It most resembles somehow casting an object to a
class it has no relationship with, and then calling a method of that
class on that object which looks for instance variables that don't
exist. I'd expect weird results there - of course, it'd also be
nice if the language prevented you from doing that sort of thing, as
most languages do.
But this bit of code seems to demonstrate that closures do exist:
on makeAdder(addend)
script adder
on add(y)
return (addend + y)
end add
end script
return adder
end makeAdder
set x to makeAdder(5)
set y to makeAdder(10)
set addend to 15
tell x to display dialog add(4) -- 9
tell y to display dialog add(6) -- 16
Even without creating any properties on the script object inside
makeAddr, its handler nevertheless remembers the value of the parameter
"addend" when called later - even though that variable is local to
makeAddr() and otherwise should cease to exist once that handler
returns. Setting a variable named "addend" outside of makeAddr()
also has no effect on the returned script objects.
It's true that these are not quite closures because, again, we're
passing around script objects and calling a known method on them,
rather than just passing around handlers. Nevertheless,
it's clear that the lexically scoped variable context is being stored
along with the object. Looks like a closure, quacks like a
closure . . .
It would be nice if makeAdder could just return adder's add directly
instead of returning the script object itself, but that doesn't quite
work, and I'm not sure why. This code:
on makeAdder(addend)
script adder
on add(y)
return (addend + y)
end add
end script
return adder's add
end makeAdder
set x to makeAdder(5)
x(1)
Dies with the y highlighted in add's "return" line and the message
"Can't make <<handler makeAdder>> into type number."
--
Mark J. Reed <
email@hidden>
_______________________________________________
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