Thursday, December 16, 2004

Closure and Continuation

Many good languages, such as JavaScript, Lisp, Smalltalk, etc., but not Java, support closure, which is basically a function (withthe bindings for its parameters) that is a first-class value that can be passed around into other functions/methods. Closure gives the developer tramendous power in writing simpler code.

According to Guy L. Steele (of Common Lisp and Scheme fame),
A Closure needs to retain information about lexical variables, but not return-address information. A continuation needs both.
So a continuation is basically a closure with the extra information of next address to go to. Supporting continuation is quite a hot topic because it greatly simplifies page flow development.

In Cornerstone, an invocation is a closure. When an invocation is put into an invocation controller, it is equivalent to putting it in an expression with lazy evaluation: only when the path of the invocation is executed, the invocation is invoked. Also, an invocation controller basically implements a new language contruct that is otherwise not available in Java. For example, the IfThenElse controller is equavlent to Smalltalk's Boolean>>ifTrue:else: method.

Not only that, a Cornerstone action has an exit, which is the next address to go to. So an action (a special case of invcation) is equivalent to continuation (which is a special case of closure with its extra next address).

Therefore, we inadverdently added support for closure and continuation to Java with invocations and actions, although not directly to the Java language. We need to explore deeper support of continuation in Cornerstone to make web development as simple as possible.