Re: functions as sort of first class objects
Re: functions as sort of first class objects
- Subject: Re: functions as sort of first class objects
- From: Axel Luttgens <email@hidden>
- Date: Thu, 26 May 2016 13:17:12 +0200
> Le 26 mai 2016 à 01:44, Mitchell L Model a écrit :
>
> […]
>
> I would love to hear an explanation of this, though I realize I am heading away from AppleScript life as we know it on this one.
>
> […]
I guess all of this goes more about syntactic matters; my theory is as follows… ;-)
Properties and handlers are top-level entities of a script (for such purposes, handlers may even be viewed as a special case of properties); the parser views a statement such as "g()" as a handler invocation when symbol "g" itself is recognized as a top-level entity.
This is perhaps best viewed by explicitly putting all the pieces into a script object:
script S1
property g : null
to a()
display alert "A"
end a
to b()
display alert "B"
end b
to show()
repeat with f in {a, b}
set g to the contents of f
g()
end repeat
end show
end script
S1’s show()
This works because of two things:
- "g" is a script’s top-level symbol
- statement "g()" recognizes symbol "g" thanks to lexical scoping
The second assertion may be checked by moving the definition of "g" after the definition of the "show" handler (in that case, the parser needs some additional hint).
That said, the parser will also recognize "g()" as a handler invocation if "g" itself is recognized as a global variable. As you did, script S1 could thus be rewritten as:
script S2
to a()
display alert "A"
end a
to b()
display alert "B"
end b
to show()
global g
repeat with f in {a, b}
set g to the contents of f
g()
end repeat
end show
end script
S2's show()
But… things tend to become muddier, because of the very special handling of global variables in AppleScript. And, from a strictly practical point of view, script S1 tends to be a self-contained entity, while script S2 has side effects.
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden