View source code
Display the source code in std/algorithm/comparison.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aGitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Function std.algorithm.comparison.castSwitch

Executes one of a series of handlers based on the dynamic type of a class instance.

auto castSwitch(choices...)(
  Object switchObject
);

switchObject's dynamic type is checked for compatibility with the argument type of each choice in turn until a match is found. When there's a match, the choice will be called with switchObject cast to that type. If the choice returns a value, castSwitch will return it.

- If one choice's return type is void and one can return a value, SwitchError will be thrown if the void handler was matched and completed execution without throwing. - A void handler is allowed to return when all the choices' return types convert to void - in that case, castSwitch itself will return void. - A choice's return type can be noreturn (e.g. when a handler always throws an exception).

Throws

If none of the choices match, a SwitchError will be thrown. SwitchError will also be thrown if one choice can return a value but a void choice was matched and completed execution.

Parameters

NameDescription
choices A sequence of function and/or delegate handlers that each accept one argument. There can also be one choice that accepts zero arguments, which will be invoked if the switchObject is null.
switchObject the object against which the tests are being made.

Returns

The return value of the selected choice.

Example

import std.algorithm.iteration : map;
import std.format : format;

class A
{
    int a;
    this(int a) { this.a = a; }
}
interface I { }
class B : I { }

Object[] arr = [new A(1), new B(), null];

auto results = arr.map!(castSwitch!(
                            (A a) => "A with a value of %d".format(a.a),
                            (I i) => "derived from I",
                            ()    => "null reference",
                        ))();

// A is handled directly:
writeln(results[0]); // "A with a value of 1"
// B has no handler - it is handled by the handler of I:
writeln(results[1]); // "derived from I"
// null is handled by the null handler:
writeln(results[2]); // "null reference"

Example

Using with noreturn/void handlers:

import core.exception : SwitchError;
import std.exception : assertThrown;

class A { }
class B { }

// B's handler never returns, so `i` does not need a value
int i;
assertThrown!Exception(
    i = new B().castSwitch!(
        (A a) => 1,
        (B b) { throw new Exception("B is not allowed!"); }
    )()
);

// Void handler call will throw if another handler returns a value
assertThrown!SwitchError(
    i = new B().castSwitch!(
        (A a) => 1,
        (B b) {}
    )()
);

// Void handlers are allowed if all the handlers convert to void:
new A().castSwitch!(
    (A a) { },
    (B b) { assert(false); },
)();

Authors

Andrei Alexandrescu

License

Boost License 1.0.