[Issue 9206] std.exception.isValidEnumValue

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 25 15:53:49 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9206



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-01-25 15:53:47 PST ---
(In reply to comment #0)
> @property bool isCompleteIntervalEnum(E)()
> pure nothrow if (is(E == enum)) {
>     static if (isIntegral!E || isSomeChar!E) {
>         return EnumMembers!E.length == (E.max - E.min + 1);
>     } else {
>         return false;
>     }
> }

That's an interesting optimization.

> bool isValidEnumValue(E)(E e)
> pure nothrow if (is(E == enum)) {
>     static if (isCompleteIntervalEnum!E) {
>         return e >= E.min && e <= E.max; // Optimization.
>     } else {
>         return [EnumMembers!Foo].canFind(e);
>     }
> }

A mixed-in switch will be slightly faster in the else clause, except in the
case of floating-point which can't be used with switches.

Anyway I think it's worth adding.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list