[Issue 21334] New: std.conv.to could have an overload taking a default value for conversion errors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 21 05:30:25 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21334

          Issue ID: 21334
           Summary: std.conv.to could have an overload taking a default
                    value for conversion errors
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: andrej.mitrovich at gmail.com

I would really like this functionality in Phobos:

-----
void main() nothrow
{
    enum OP : ubyte
    {
        Invalid = 0,
        OP1 = 1,
        OP2 = 2
    }

    ubyte bad = 4;
    OP op = to!(OP, OP.Invalid)(bad);
    assert(op == OP.Invalid);
}
-----

In case of conversion errors `to` would return the provided value (and
therefore be nothrow).

We could alternatively introduce a new function rather than modify `to`.

--


More information about the Digitalmars-d-bugs mailing list