Nothrow std.conv.to with explicit default value

Adam D. Ruppe destructionator at gmail.com
Mon Jun 18 21:04:55 UTC 2018


On Monday, 18 June 2018 at 20:48:55 UTC, Per Nordlöw wrote:
> T toDefaulted(T, S, U)(S value, /*lazy*/ U defaultValue)
> if (is(typeof(() { T r = defaultValue; }))) // TODO use 
> std.traits.isAssignable!(T, U) ?

why not just make it

T toDefaulted(T, S)(S value, T defaultValue)

and forget U entirely?

> The problem with this code is that throwing exceptions for the 
> default case is costly at least with dmd. Is there another way 
> to do this?

It depends on what the types are. If it is like string to int, 
you can simply scan the string for the appropriate format.

For other conversions though, I don't think you can tell in 
general. User-defined types might not even tell you except by 
exception.


More information about the Digitalmars-d-learn mailing list