Nothrow std.conv.to with explicit default value

Per Nordlöw per.nordlow at gmail.com
Wed Jun 20 09:54:29 UTC 2018


On Wednesday, 20 June 2018 at 09:52:04 UTC, Per Nordlöw wrote:
> My suggestion for nothrow @nogc string-to-enum conversion with 
> default value
>
>
> T toDefaulted(T)(scope const(char)[] value,
>                  T defaultValue) @safe pure nothrow @nogc
> if (is(T == enum))
> {
>     switch (value)
>     {
>         static foreach (index, member; __traits(allMembers, T))
>         {
>         case member:
>             return cast(T)(index);
>         }

Oops, this doesn't work for enums with "holes". How do I most 
easily fix that?


More information about the Digitalmars-d-learn mailing list