BRiX
Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Functions ----- Up: Contents ----- Next: Extensions

Closures

A closure is an anonymous function that traps the values of variables and parameters from its parent's scope. Instead of returning a pointer to a function the definition returns a pointer to a closure object that has the same interface as the function. This object is dynamically allocated and populated with the trapped values. The closure object header contains an instruction to jump to the code block. This object can be passed around as it if were a simple anonymous function pointer and the caller requires no extra knowledge of the closure data.

The language hides variables and parameters from parent scopes when defining functions and a special operator is required to access and trap them. The prefix $ operator will lookup the identifier in the parent's scope and begin the closure creation process. The only difference between a normal anonymous function and a closure is the usage of $.

	foo := def (s:String) {
	  b:TextButton = "click me for message"
	  b.callback = def (b:TextButton) { b.title = $s; b.callback = null }
	  return b
	}

Previous: Functions ----- Up: Contents ----- Next: Extensions