Mike Austin's Blog

Saturday, October 08, 2005

Benefits of multi-methods

I've mostly used and played around with C++, Ruby and Python which have no support for multi-methods. Now that OpenDylan is available, I can use it in a language which I admire. Here's how you can respond to an event in Dylan:
define method handle-event( event :: <button-down>, button == $left ) => ()
[...]
end method;

define method handle-event( event :: <button-down>, button == $right ) =>()
[...]
end method;

And here's the equivalent in pseudo-Java:

void handle_event( Event event, int button ) {
if( event.isInstanceOf( ButtonDown ) ) {
if( button == LEFT_BUTTON ) {
[...]
} else if( button == RIGHT_BUTTON ) {
[...]
}
}
}
That is an entangled mess of spaghetti.

3 Comments:

Post a Comment

<< Home