Search⌘ K
AI Features

Delegate Properties, Lazy Parameters & Lazy Variadic Functions

Explore how to access delegate properties and create delegates from scratch in D programming. Understand lazy parameters, which are delegates created by the compiler for deferred evaluation, and learn how to handle lazy variadic functions for variable numbers of lazy parameters of the same type. This lesson helps you improve your code efficiency and flexibility with advanced function pointer management.

Delegate properties

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

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:

D
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 ...