2.2 Methods
body ::= {stmt} result | empty return(The syntax and semantics of statements and expressions is described in section 2.5.)void
result ::= normal_return return an expression | non_local_rtn return from the lexically-enclosing method normal_return ::= decl_block [";"] returnvoid
| assignment [";"] returnvoid
| expr [";"] return result of expression non_local_rtn ::= "^" [";"] do a non-local return, returningvoid
| "^" expr [";"] do a non-local return, returning a result
When invoked, a method evaluates its statements in a new environment containing bindings for the method's formal parameters and nested in the method's lexically-enclosing environment. (The interactions among nested scopes, method lookup, and other language features is described in more detail in section 2.7.7.)
The result of the message invoking the method is the result of the last statement in the method's body. If the method's body is empty, then the method returns the special void
value. Alternatively, a method returns void
if the last statement is a declaration block, an assignment statement, or an expression that itself returns void
. The void
value is used to indicate that the method returns no useful result. The system ensures that void
is not accidentally used in later computation by reporting an error (statically in the presence of type checking) if void
is passed as an argument to a message.
When a closure's eval
method is invoked, evaluation proceeds much like the evaluation of any other method. One difference is that a closure eval
method may force a non-local return by prefixing the result expression with the ^
symbol; if the result expression is omitted, then void
is returned non-locally. A non-local return returns to the caller of the closest lexically-enclosing non-closure method rather than to the caller of the eval
method, just like a non-local return in Smalltalk-80[3] [Goldberg & Robson 83] and Self and similar to a return
statement in C. The language currently prohibits invoking a non-local return after the lexically-enclosing scope of a closure has returned; first-class continuations are not supported.
Generated with Harlequin WebMaker