...
/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.
We'll cover the following...
We'll cover the following...
Delegate properties
The function and context pointers of a delegate can be accessed through its .funcptr
and .ptr
properties, respectively:
Press + to interact
D
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
D
import std.stdio;struct MyStruct {int i;void func() {writeln(i);}}void main() {auto o = MyStruct(42);void delegate() d;// null to begin withassert(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
on o
).