[Next] [Previous] [Up] [Top] [Contents] [Index]

2.5 Statements and Expressions

2.5.9 Closures

The syntax of a closure constructor expression is as follows:

closure_expr	::=	[ "&" "(" [closure_formals] ")" ] "{" body "}"
closure_formals	::=	closure_formal { "," closure_formal }
closure_formal	::=	[name]	formal names are optional, if never referenced
This syntax is identical to that of a method declaration, except that the method keyword and message name are replaced with the & symbol (intended to be suggestive of the l symbol). If the closure takes no arguments, then the &() prefix may be omitted. When evaluated, a closure constructor produces two things:

As with other nested method declarations, the body of a closure's eval method is lexically-scoped within the scope that was active when the closure was created. However, unlike nested method declarations, the eval method is globally visible (as long as the connected closure object is reachable). Closures may be invoked after their lexically-enclosing scopes have returned.[8]

All control structures in Cecil are implemented at user level using messages and closures, with the sole exception of the loop primitive method described in section 2.2.3. Additionally, closures can be used to achieve much the same effect as exceptions and multiple results, so these other constructs are currently omitted from the Cecil language. Sometimes the use of closures is syntactically more verbose than a built-in language construct might be, and we are considering various alternatives for allowing programmers to define syntactic extensions to the language to provide a cleaner syntax for their user-defined control structures.


[8] In the current UW Cecil implementation, there are some caveats to the use of such non-LIFO closures. See the system documentation for additional details.
The Cecil Language: Specification and Rationale, Version 2.1 - 25 MARCH 1997
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker