FlexibleDelegate

Declares a delegate property with several setters to allow for handlers that don't care about the arguments.

Throughout the arsd library, you will often see types of these to indicate that you can set listeners with or without arguments. If you care about the details of the callback event, you can set a delegate that declares them. And if you don't, you can set one that doesn't even declare them and it will be ignored.

Members

Functions

opAssign
void opAssign(DelegateType dg)
void opAssign(ReturnType delegate() dg)
void opAssign(ReturnType function(Parameters params) dg)
void opAssign(ReturnType function() dg)
void opAssign(typeof(null) explicitNull)

These opAssign overloads are what puts the flexibility in the flexible delegate.

opCall
ReturnType opCall(Parameters args)

Calls the currently set delegate.

opCast
bool opCast()

Use if(thing) to check if the delegate is null or not.

Examples

// you don't have to put the arguments in a struct, but i recommend
// you do as it is more future proof - you can add more info to the
// struct without breaking user code that consumes it.
struct MyEventArguments {

}

// then you declare it just adding FlexibleDelegate!() around the
// plain delegate type you'd normally use
FlexibleDelegate!(void delegate(MyEventArguments args)) callback;

// until you set it, it will be null and thus be false in any boolean check
assert(!callback);

// can set it to the properly typed thing
callback = delegate(MyEventArguments args) {};

// and now it is no longer null
assert(callback);

// or if you don't care about the args, you can leave them off
callback = () {};

// and it works if the compiler types you as a function instead of delegate too
// (which happens automatically if you don't access any local state or if you
// explicitly define it as a function)

callback = function(MyEventArguments args) { };

// can set it back to null explicitly if you ever wanted
callback = null;

// the reflection info used internally also happens to be exposed publicly
// which can actually sometimes be nice so if the language changes, i'll change
// the code to keep this working.
static assert(is(callback.ReturnType == void));

// which can be convenient if the params is an annoying type since you can
// consistently use something like this too
callback = (callback.Parameters params) {};

// check for null and call it pretty normally
if(callback)
	callback(MyEventArguments());

Meta