Multimethod Dispatch with Continuation Passing Style

This made me stop and gasp:

Now, even a cursory inspection will show that the function call and return continuation invocation are the same thing. What does this mean? Well, of course it means you can do MMD on function returns as well as on function calls.

Just think of the possibilities! Not that any possibilities are occuring to me at the moment... but just think anyway!

Posted on August 19, 2004 07:13 PM
More languages articles

Comments

Can't think of any? Can't think of any? Sheesh!

This means you can make tail calls in one statement where you'd need many before. This:

   result = foo()
if (result.isa(string)) {
bar();
} else {
baz();
}
becomes:
   foo() :returns(string) => bar() :returns(any) => baz();
an obvious increase in program clarity and expressiveness!

More global dispatching operations can be done by annotating functions -- do something like:

   void foo(int a, int b) :come_from(any, int, int) {
   }

and any time any function returns two integers it automatically dispatches to foo, rather than whatever code called that function!

With math operations are all really MMD-dispatched functions, it means that you can treat + as a control flow operator. The possibilities boggle the mind!

(Hey, I never said that any of the possibilities were sane... :)

More seriously, though, you could use this to wedge code in transparently. If the return continuation you're invoking that gets usurped this way is passed into the usurping function as its return continuation (assuming some protection in to avoid infinite loops) it'd let you install wrappers on function returns with some selectivity. I hear the AOP folks are big on that sort of thing. (Whether this is an argument for or against implementing this is an open question)

Posted by: Dan at August 20, 2004 07:19 AM

You could also implement all of your exception handlers in this manner. They are just alternate multimethods for exceptional return values... And thus throw becomes merely return (or actually invoke return multimethod).

Matt
--
"Computer Science is merely the post-Turing decline of Formal Systems Theory"
-???

Posted by: Matt Fowles at August 23, 2004 09:15 PM
Post a comment









Remember info?




Prove you're human. Type "human":