toDefaulted

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

  1. T toDefaulted(S value, U defaultValue)
    nothrow
    T
    toDefaulted
    (
    T
    S
    U
    )
    (
    const scope S value
    ,)
    if (
    !is(T == enum) &&
    is(typeof(
    ()
    ))
    )
  2. T toDefaulted(const(char)[] value, T defaultValue)

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