www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21334] New: std.conv.to could have an overload taking a


          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 puremagic.com
          Reporter: andrej.mitrovich 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`.

Oct 20 2020