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)
    @safe pure nothrow @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);

See Also

Meta