...

/

Delegate Properties, Lazy Parameters & Lazy Variadic Functions

Delegate Properties, Lazy Parameters & Lazy Variadic Functions

You will learn about delegate properties, lazy parameters, and lazy variadic functions in this lesson.

Delegate properties

The function and context pointers of a delegate can be accessed through its .funcptr and .ptr properties, respectively:

Press + to interact
import std.stdio;
struct MyStruct {
void func() {
}
}
void main() {
auto o = MyStruct();
auto d = &o.func;
writeln(d.funcptr == &MyStruct.func);
writeln(d.ptr == &o);
}

It is possible to make a delegate from scratch by setting those properties explicitly:

Press + to interact
import std.stdio;
struct MyStruct {
int i;
void func() {
writeln(i);
}
}
void main() {
auto o = MyStruct(42);
void delegate() d;
// null to begin with
assert(d is null);
d.funcptr = &MyStruct.func;
d.ptr = &o;
d();
}

Above, calling the delegate as d() is the equivalent of the expression o.func() (i.e., calling MyStruct.func ...