Closure
ceramic.Closure (Class)
A simple closure implementation for storing a function with pre-bound arguments.
This class provides a way to capture a function reference along with its arguments, allowing for delayed execution. Useful for callbacks, event handlers, and situations where you need to pass a pre-configured function.
Usage Example
// Store a function with arguments
function greet(name:String, age:Int) {
trace('Hello $name, age $age');
}
var closure = new Closure(greet, ["Alice", 30]);
// Execute later
closure.call(); // Outputs: "Hello Alice, age 30"
// Can also store instance methods
var closure2 = new Closure(myObject.doSomething, [param1, param2]);
closure2.call();
See: ceramic.Timer For delayed execution, ceramic.App#onceImmediate For deferred execution
Instance Members
method: Any
The function or method to be called. Can be a static function, instance method, or any callable reference.
Arguments to pass to the method when called. These are bound at construction time and passed during execution.
call(): Dynamic
Executes the stored method with the bound arguments.
Uses reflection to call the method, which allows it to work with any type of function or method reference.
Returns | Description |
---|---|
Dynamic | The return value from the called method, or null if the method returns Void * haxe var closure = new Closure(Math.max, [5, 10]); var result = closure.call(); // Returns 10 |
Creates a new closure with the specified method and arguments.
Name | Type | Default | Description |
---|---|---|---|
method |
Any | The function or method to store. Can be any callable reference. | |
args |
Array<Any> | (optional) | Optional array of arguments to pass when the method is called. If not provided, an empty array is used. * haxe // Simple function var c1 = new Closure(trace, ["Hello"]); * // Instance method var c2 = new Closure(sprite.moveTo, [100, 200]); * // No arguments var c3 = new Closure(doCleanup); |