enum

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 9 11:14:11 PDT 2014


On Wednesday, April 09, 2014 08:38:54 Andrei Alexandrescu wrote:
> Too restrictive. What is a valid enum value? Would an enum flags need to
> ascribe a name to every possible combination?

Why is that too restrictive? I don't see how it even fundamentally makes sense 
for

    auto result = MyEnum.a | MyEnum.b;

to result in a value of type MyEnum. It _might_ result in a valid enum value, 
but if an enum is supposed to represent a specific set of flags or constants, 
in the general case, I would expect that doing any arithmetic operation on 
them (or concatenating to them in the case of something like strings) would 
result in a value that was not a valid enum value, and I don't see how anyone 
could really expect that code like

    MyEnum result = MyEnum.a | MyEnum.c;
    StringEnum str = SringEnum.s ~ " foo";

could be considered valid except in rare cases, and in those cases, you could 
simply cast the result to the enum type since you know that it's going to 
result in a valid enum value. I fail to see why you'd even _want_ the result
of operations on an enum to result in the same type given that it's almost
a guarantee that it won't result in one of the enumerated values.

- Jonathan M Davis


More information about the Digitalmars-d mailing list