Implicit enum conversions are a stupid PITA

yigal chripun yigal100 at gmail.com
Wed Mar 24 13:45:23 PDT 2010


Regan Heath Wrote:

> 
> One thing being able to convert enum to it's base type does allow is this:
> 
> import std.stdio;
> 
> enum FLAG
> {
>    READ  = 0x1,
>    WRITE = 0x2,
>    OTHER = 0x4
> }
> 
> void foo(FLAG flags)
> {
>    writeln("Flags = ", flags);
> }
> 
> int main(string[] args)
> {
>    foo(FLAG.READ);
>    foo(FLAG.READ|FLAG.WRITE);
>    return 0;
> }
> 
> 
<snip>

Here's a Java 5 version with D-like syntax: 

enum Flag {
    READ  (0x1), WRITE (0x2), OTHER(0x4)
    
    const int value;
    private this (int value) {
        this.value = value;
     }
}

 int main(string[] args) {
    foo(FLAG.READ.value);
    foo(FLAG.READ.value | FLAG.WRITE.value);
    return 0;
}

No conversions required. 



More information about the Digitalmars-d mailing list