yage.core.closure
Authors:
Eric Poggel
License:
LGPL v3
- ClosureHelper!(T) closure
(T, A...)(T func, A a);
- Create a closure
-- a function with arguments that can be called later, even when
the arguments go out of scope.
Params:
func |
The function to call. This can also be a class methods
so long as they aren't static. For now, static method calls must
be wrapped inside a non-static function. |
var_args |
A variable number of arguments to bind to the function.
Does not support out or inout arguments. |
Returns:
An object that can be called via opCall(), which calls the original
function
Example:
Closure c = closure((int a, char[] b)
{ writefln(a, b);
}, 3, "Hello");
// Later, after a and b go out of scope
c(); // writes "3Hello"
// Alternatively, we can use functions (even class methods) instead of delegates.
class Foo
{ int bar(int a,)
{ return a;
}
}
Foo f = new Foo();
Closure c = closure(&f.bar, 3);
// Later, after f goes out of scope.
writefln(c());
- abstract interface Closure
;
- Allows working with the various templated return types of
closure() as a single type. This is useful for creating an arry of
closures, for example. However, this doesn't allow getting any
return values, since D doesn't allow overloading by return type.
|