- Objects and Messages
- Higher Order Functions
- From Functions to Messages
- Forwarding Messages
- Object Curry
From Functions to Messages
In both Smalltalk and Objective-C, messages are objects, so everything possible with an object — including introspection — is available in a message. This design allows for a very simple way of implementing something like a map function in Objective-C. The map function will take two arguments, but in an object-oriented language one of the arguments is implicit — the receiver. Rather than calling a map function with a collection as an argument, we want to send a map message to a collection object. Just as the argument to a map function is a function, the argument to a map message should be a message. But this gets a bit more difficult; how do you send a message as an argument?
At this point, it's important to distinguish between a message and a selector. NSArray has two methods that look a little bit like a map function and take a selector as an argument:
These methods work for messages that take zero or one object arguments, but what about messages that take more? What about messages that take structures or floating-point arguments? I suppose we could implement a method like this:
But the power of a dynamic language like Objective-C is the ability to write generic code. Ideally, we want to be able to do something like this:[[array map] setFrame: someRect];
This is an example of higher-order messaging; the second message appears to be an argument to the first. Obviously, this isn't really what happens, though; so how does it work?