...

/

opDispatch() and opBinaryRight()

opDispatch() and opBinaryRight()

Now, let’s talk about opDispatch() and opBinaryRight() functions.

Catch-all operator opDispatch()

opDispatch gets called whenever a missing member of an object is accessed. All attempts to access non-existent members are dispatched to this function.

The name of the missing member becomes the template parameter value of opDispatch.

The following code demonstrates a simple definition:

Press + to interact
import std.stdio;
struct Foo {
void opDispatch(string name, T)(T parameter) {
writefln("Foo.opDispatch - name: %s, value: %s",
name, parameter);
}
}
void main() {
Foo foo;
foo.aNonExistentFunction(42);
foo.anotherNonExistentFunction(100);
}

There are no compiler errors for the calls to non-existent members. Instead, all of those calls are dispatched to ...