• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Handlers in a variable
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handlers in a variable


  • Subject: Re: Handlers in a variable
  • From: has <email@hidden>
  • Date: Sat, 16 Jan 2016 14:14:30 +0000

Nigel Garvey wrote:

> "Mark J. Reed" wrote on Fri, 15 Jan 2016 22:19:42 -0500:
>
>>     to call(aHandler, aValue)
>>        aHandler(aValue)
>>     end
>>
>>     call(double, 4)
>>
>>     error "«script» doesn’t understand the “aHandler” message." number
>> -1708 from «script»
>
> Hi Mark.
>
> The handler has to be transferred to a non-local variable and called
> with that. eg.: [...]

Except as we just discussed last month: don't do that (see also: http://weblogs.asp.net/alex_papadimoulis/408925). AS handlers aren't closures, and moving them around like this breaks all their free variables. Absence of classes aside, AS's object system works like Smalltalk's, not JavaScript's: you send a command (message) to a [script] object, and it's up to the object to dispatch it to the corresponding handler if it has one, or else delegate the command to its parent object if it doesn't.

The *correct* way for Mark to do what he wants to do is to pass a script object containing the handler(s) you want to call - that's not a "hack", it's how the language was designed to work. e.g. See the Sort suite of the ListLib library at <https://github.com/hhas/applescript-stdlib/archive/master.zip>, which does exactly this. (It's not an ideal example as the implementation's all crudded up with script object-based speed hacks, but just ignore those.) The `sort list` handler optionally takes a script object containing `makeKey` and/or `compareKeys` handlers that allow its sorting behavior to be customized. It even includes ready-made script objects for the most common use cases (lists of numbers, texts, or dates), while users can define their own to perform more complex operations (e.g. sorting a list of records on certain properties).

has

_______________________________________________
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


  • Prev by Date: Re: Handlers in a variable
  • Next by Date: Re: AppleScript lists are O(1) vectors (was Re: AS Library Question)
  • Previous by thread: Re: Handlers in a variable
  • Next by thread: Re: Handlers in a variable
  • Index(es):
    • Date
    • Thread