edu.vub.at.objects
Interface Closure

All Known Subinterfaces:
JavaClosure

 Closure

ATClosure is the public interface to a native AmbientTalk closure (a method + enclosing environment).

Since Methods are always wrapped either at creation time (blocks) or during lookup (methods), ATClosures are by definition the only way methods and blocks can be encountered at the AmbientTalk base level.

Closures should respond to the base_apply method, which should trigger the invocation of their encapsulating method in the enclosed closure context.

Closures are sometimes also 'abused' to simply represent blocks of source code whose body has to be evaluated not in the enclosed lexical context, but within the context of another object. To facilitate such use, a closure provides the method 'base_applyInScope' which will execute the enclosed method in the scope of the given object, rather than in the enclosed lexical context.

Author:
smostinc, tvcutsem

Method Summary
 Object apply(Table args)
          Applies the closure to the given arguments, already wrapped in a table.
 Object applyInScope(Table args, Object scope)
          Applies the closure to the given arguments, already wrapped in a table.
 Context context()
          Returns the scope of the closure.
 Object escape()
          Escape control construct { |quit| ... quit(val) ... }.escape() The escape control construct passes to its receiver block a function which when invoked, immediately transfers control back to the caller of escape, returning the value passed to quit.
 Method method()
          Returns the encapsulated method.
 Object whileTrue:(Closure body)
          Allows AmbientTalk programmers to write { booleanCondition }.whileTrue: { body } which will execute body as long as the boolean condition evaluates to true.
 
Methods inherited from interface edu.vub.at.objects.Object
super
 

Method Detail

method

Method method()
Returns the encapsulated method.

Returns:
an Method that returns the encapsulated method.

context

Context context()
Returns the scope of the closure.

Returns:
an Method that returns the scope of the closure.

apply

Object apply(Table args)
Applies the closure to the given arguments, already wrapped in a table. The enclosed method is executed in the context provided by the closure.

Parameters:
args - the already evaluated arguments, wrapped in a table.
Returns:
the value of evaluating the method body in the context of the closure.

applyInScope

Object applyInScope(Table args,
                    Object scope)
Applies the closure to the given arguments, already wrapped in a table. The enclosed method is executed in the context of the given object. The enclosed closure context is disregarded.

The context provided by an object is always equal to: ctx(cur=object,self=object,super=object.dynamicParent)

Parameters:
args - the already evaluated arguments, wrapped in a table.
scope - the object that will act as self and as lexically enclosing scope.
Returns:
the value of evaluating the method body in the context of the given object scope.

whileTrue:

Object whileTrue:(Closure body)
Allows AmbientTalk programmers to write { booleanCondition }.whileTrue: { body } which will execute body as long as the boolean condition evaluates to true.

More specifically, what the native implementation (expressed in AmbientTalk syntax) does is:

def whileTrue: body { self.apply().ifTrue: { body(); self.whileTrue: body } }

Parameters:
body - the block of code that will be executed as long as the boolean condition evaluates to true.
Returns:
the value of the last closure applied.
Throws:
edu.vub.at.exceptions.InterpreterException - if raised inside the code closure.

escape

Object escape()
Escape control construct { |quit| ... quit(val) ... }.escape()

The escape control construct passes to its receiver block a function which when invoked, immediately transfers control back to the caller of escape, returning the value passed to quit.

If no value is passed to quit, nil is returned instead.

If quit is not invoked during the execution of the receiver block, the block terminates normally, with its normal return value.

If quit is invoked at the point where the call to escape has already returned, either normally or via an exception or another escape call, invoking quit will raise a IllegalOperation exception.

Returns:
the value passed to quit.