toDefaulted

Variant of std.conv.to with defaultValue making it nothrow.

  1. T toDefaulted(S value, U defaultValue)
  2. T toDefaulted(const(char)[] value, T defaultValue)
    pure nothrow @safe @nogc
    T
    toDefaulted
    (
    T
    )
    (
    scope const(char)[] value
    ,
    const T defaultValue
    )
    if (
    is(T == enum)
    )

Examples

assert("42_1".toDefaulted!int(43) == 43);
assert(42.toDefaulted!string("_43") == "42");
enum E { unknown, x, y, z, z2 = z, }
assert("x".toDefaulted!(E)(E.init) == E.x);
assert("z".toDefaulted!(E)(E.init) == E.z);
assert("z2".toDefaulted!(E)(E.init) == E.z);
assert("_".toDefaulted!(E)(E.init) == E.unknown);
enum E { unknown, x, }
assert(E.x.toDefaulted!string("init") == "x");
assert(E.init.toDefaulted!string("init") == "unknown");

See Also

Meta