callWithActive

Calls the specified callable with the active field of the provided smart-union. If no field is active, does nothing.

Note: declared at module-scope (rather than nested inside the SmartUnion template) to work around limitations of template alias parameters. (Doing it like this allows it to be called with a local name.)

void
callWithActive
(
alias Callable
SU
)

Parameters

Callable

alias for the thing to be called with the active member of the provided smart-union

SU

type of smart-union to operate on

smart_union SU

smart-union instance whose active field should be passed to Callable

Examples

// This example is D2 only because it requires a function template,
// `print`, and D1 doesn't allow defining a function template at the scope
// of a function, including a `unittest`. In D1 this example works if
// `print`, is defined outside of function scope.

union TestUnion
{
    int a;
    float b;
}
alias SmartUnion!(TestUnion) TestSmartUnion;

static struct ActiveUnionFieldPrinter
{
    static void print ( T ) ( T t )
    {
        Stdout.formatln("{}", t);
    }

    void printActiveUnionField ( )
    {
        TestSmartUnion su;
        su.a = 23;
        callWithActive!(print)(su);
    }
}

Meta