2.5 Statements and Expressions
closure_expr ::= [ "&" "(" [closure_formals] ")" ] "{" body "}" closure_formals ::= closure_formal { "," closure_formal } closure_formal ::= [name] formal names are optional, if never referencedThis 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:
closure
object, which is returned as the result of the closure constructor expression, and
eval
whose anonymous first argument is specialized on the newly-created closure object and whose remaining arguments are those listed as formal parameters in the closure constructor expression.
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.
Generated with Harlequin WebMaker