Bug 21334 – std.conv.to could have an overload taking a default value for conversion errors

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-10-21T05:30:25Z
Last change time
2022-07-04T17:12:14Z
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2020-10-21T05:30:25Z
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`.
Comment #1 by andrej.mitrovich — 2022-07-04T17:12:14Z
I think this will be resolved as part of making Phobos more @nogc. Adding special overloads like this might not be worth it and could make the API harder to reason with.