/customers/iconara.net/iconara.net/httpd.www/blog/wp-content/plugins/wp-super-cache/wp-cache-phase1.php Iconara » Higher order messaging

Higher order messaging

Higher order messaging (HOM) is a concept from the world of dynamic object oriented programming languages like Objective-C and Smalltalk. In this article I’m introducing the concept to ActionScript/JavaScript. It’s quite a theoretical concept, but it’s implementation can be very useful, and because of that, I’ll try to not get too boring.

Higher order messaging is similar to passing functions as arguments to functions, as you can do in ActionScript (but not that many other languages, actually), and it’s this aspect we are going to look further into.

Iteration

Say you have a list of objects, and that you want to call the same method on each of them, a not so unusual situation. How would you do it? Most likely as a very simple for-loop.

Let’s say we have an array of spaceship-objects, which have a method called setShieldStrength, and we want all of the spaceships to have the same shield strength, namely 100:

for ( var i = 0; i < spaceships.length; i++ ) {
    spaceship[i].setShieldStrength(100);
}

A block of everyday code, don’t you think?

I think it’s ugly. It’s very low level, I hate to have to think about a counter for how many spaceships there are, I never really use the counter anyway, it’s just necessary evil. It’s not very OO (not that being OO is a goal in itself, it’s just that generally OO concepts simplify things). In proper OO API:s, the need for for-loops for this kind of thing is done away with, thanks to the design pattern “iterator”. You have probably seen this is Java:

Iterator i = spaceships.iterator();

while ( i.hasNext() ) { Spaceship s = (Spaceship) i.next();

s.setShieldStrength(100);

}

Now we don’t have to bother with things as size, and if we decide to store the spaceships as something other than an array, we don’t have to change a lot of code as a result. The iteration has been abstracted, and abstraction is a good thing. There’s also the for-each/for-in-loop, which works in a similar way.

Let’s get to the point of this article: we can abstract it even further. It’s not that iterators are bad in any way, but there’s still a lot of code there that is the same every time, and I’m lazy enough to try to find a quicker way to code iteration, but at the same time retain the level of abstraction. I get borded by writing while-loops. This is where higher order messaging comes in. How about rewriting the code above into this:

spaceships.all().setShieldStrength(100);

Wouldn’t that be great? It’s very possible to do. This is higher order messaging: using a message (read: method call) as argument to a message. In this case the message all() gets the argument setShieldStrength(100), perhaps it doesn’t look like an argument, but conceptually it is. What is does is that all() returns a proxy object (called a “trampoline”), which recieves the setShieldStrength(100) message and sends it to each object in the spaceships array. It’s quite clear what the line does and there’s no superflous code (no declaration of variables, no control of flow, etc.). It’s clear and concise and that’s why I like it.

There’s more to it, though. all() is just scratching on the surface. Conceptually similar are collect(), which returns a list of all the return values of the method call, select() and reject(), which returns the return the objects that returned true or false, respectively, from the method call.

Conclusion

In conclusion, HOM is a powerful concept. In this article we have seen how to use it to abstract iteration, which is the most common use. However, trampolines and proxies an be used for so much more, and maybe my examples can serve as inspiration of what can be done. ActionScript is a highly dynamic language, very much more powerful than the ActionScript API:s would suggest.

I have an implementation of some of the things mentioned in this article, as a runtime patch of the Array class, and I would be happy to share it to anyone interested, mail me and I’ll send it over.

When I get around to it, I’ll write an article about code blocks, which is a similar concept to HOM, and also a powerful concept not properly exploited in ActionScript.

References

HigherOrderMessaging Message Oriented Programming

One Response to “Higher order messaging”

  1. Jay Says:

    Hi, I am interested in the javascript(array) implementation of HOM/ Can you please email me the samples to look at?

    Thank you very much. Excellent article.

    Jay

Leave a Reply