HOM

From GNUstepWiki

Revision as of 12:02, 16 September 2005; view current revision
←Older revision | Newer revision→

HOM (short for High Order Messaging) is a mechanism for encapsulating control structures and other programming patterns. It is similar to blocks in Smalltalk and higher order functions in functional languages.

Objective-C does not have blocks. They're not a part of the language, although Brad Cox's Task Master Paper outlines them nicely (and the Portable Object Compiler implements them).

However, it is possible to implement HOM (using trampolines), but HOM is not a language feature at this point.

What does HOM look like?

Suppose you have an NSArray myArray containing objects you want to call a message on. Normally you would have to do something like

 [myArray makeObjectsPerformSelector: @selector(someOtherMessage)];

With HOM you simply write

 [[myArray do] someOtherMessage];

instead.

Simply put: the -do message tells myArray to call -doSomething on each of its objects.

HOM related messages

  • -each

Iterates through each object, similar to -objectEnumerator.

  • -collect

Returns an array of the responses to the argument message.

  • -select

Returns members of the array if they respond YES to the argument message -- which necessarily returns a BOOL.

  • -reject

The inverse of -select; it returns the elements responding NO.

  • -performAfterDelay:

Performs the argument message after a specified delay.

  • -ignoreExceptions

Traps and ignores any exception that may occur during execution of the argument message.