Default-valued nothrow @nogc std.conv:to

Nordlöw via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 22 05:14:26 PDT 2017


Have anyone tried to implement a variant of `std.conv.to` that 
can be `nothrow @nogc` if the exception handling is replaced by 
an extra second parameter (`defaultValue`) returned iff the call 
to `to` throws?

I currently have a solution

https://github.com/nordlow/phobos-next/blob/b7a5c589d890f6b7bef7c5f2588fa93d90d19d62/src/conv_ex.d#L10

that is `nothrow` but not (yet) `@nogc`:


CommonType!(T, U) toWithDefault(T, U, S)(S value, U defaultValue)
     if (haveCommonType!(T, U))
{
     try
     {
         import std.conv : to;
         return value.to!T;
     }
     catch (Exception e) // assume ConvException. TODO can we 
capture ConvException instead make it inferred nothrow
     {
         return defaultValue;
     }
}

Is there any way I can make it `@nogc` without having to modify 
the code in `std.conv`?

Further, can anybody think of a more consice naming than 
`toWithDefault`?


More information about the Digitalmars-d mailing list