using enums for flags

Trass3r un at known.com
Thu Jan 26 04:58:05 PST 2012


> I agree, enum variable should only contain one of the 
> enumerated values. Here's an example how current way may lead 
> to unexpected result:
>
> enum Foo { A = 1, B }
>
> void bar( Foo foo ) {
>    final switch( foo ) {
>        case Foo.A:
>            writeln( "A" );
>            return;
>        case Foo.B:
>            writeln( "B" );
>            return;
>    }
>    writeln( "Unreachable branch" );
> }
>
> int main() {
>    bar( Foo.A | Foo.B );
>    return 0;
> }
>
> It is intuitive to assume that the final switch will always hit 
> one of it's branches, yet this doesn't work here.

Valid example.
As I said one could introduce something like @flags but I guess a 
library solution is preferred.
I still wonder though if implicit conversion to the basetype has 
any merit.


More information about the Digitalmars-d mailing list