functions as sort of first class objects
functions as sort of first class objects
- Subject: functions as sort of first class objects
- From: Mitchell L Model <email@hidden>
- Date: Wed, 25 May 2016 19:44:06 -0400
> On May 24, 2016, at 3:00 PM, Deivy Petrescu <email@hidden> wrote:
>
> Ha! This is really interesting.
> The first thing I thought was exactly that, h is only a reference to an item in the list.
> Thus, as I am used to do, I used the following:
>
> repeat with h in whatever
> set h to contents of h
> do the rest
> end repeat
>
> It turns out that in this case this fails, so if you don’t do the loop with another variable it fails.
> in other words,
>
> to a()
> display alert "A"
> end a
>
> to b()
> display alert "B"
> end b
>
> repeat with h in {a, b}
> set h to contents of h
> h()
> end repeat
> —> error "«script» doesn’t understand the “h” message." number -1708 from «script»
>
> but
>
> to a()
> display alert "A"
> end a
>
> to b()
> display alert "B"
> end b
>
> repeat with h in {a, b}
> set g to contents of h
> g()
> end repeat
>
> Unless there is something I am not fundamentally understanding, this is a bug.
Very cool! I feel like I should have figured that out, but honestly after all these many (many, many,, …) years of arguing with AppleScript, the biggest mystery to me is still when things are references and when not, and when it is necessary to get the contents of a reference and when not. I usually just flail away trying different combinations when something is mysteriously not working, but this time it just didn’t occur to me, probably because I have rarely done anything with a handler object. I also didn’t bother to look at the value of h to notice that it was a reference. So a great many thanks. I really wanted this to work.
However. I fixed my real script this way and subsequently spent several hours going back and forth expanding this one and shrinking mine, trying rewrites from scratch, etc., getting strange results and unhelpful and possibly misleading error messages. I finally got down to this beauty:
to a()
display alert "A"
end a
to b()
display alert "B"
end b
to show()
repeat with f in {my a, my b}
set g to the contents of f
g() <<<< FAILI!
end repeat
end show
show()
FAILS! «script» doesn’t understand the “g” message.
Well, that is decidedly unfair. The only difference between this and the above is that the loop is inside a handler? Could that possibly matter? (tick, tock, tick, tock — hmmm, can’t see why it would) Finally set a breakpoint a g(), but didn’t see anything interesting. Except that Eventually I realized that g wasn’t showing up in the Script Debugger Variables and Values pane. So I did what is often necessary (though I have never quite figured out when or why), which is to declare g global. And that made it work! Doubly unfair!!
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.
to a()
display alert "A"
end a
to b()
display alert "B"
end b
to show()
global g <<<< necessary!
repeat with f in {my a, my b}
set g to the contents of f
g()
end repeat
end show
show()
_______________________________________________
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