Implicit enum conversions are a stupid PITA
Regan Heath
regan at netmail.co.nz
Thu Mar 25 03:23:21 PDT 2010
yigal chripun wrote:
> 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.
Cool. I wasn't aware of that Java feature/syntax - shows how much Java
I do :p
But.. what is the definition of 'foo' in the above, specifically does it
take an argument of type Flag? or int?
If the latter, then all you're doing is shifting the conversion. In my
example it was a cast, in the above it's a property called 'value' which
converts the "enum" to 'int'.
Interestingly you can do something similar in D...
import std.stdio;
struct Enum { this(int v) { value = v; } int value; }
struct Flag
{
Enum READ = Enum(1);
Enum WRITE = Enum(2);
Enum OTHER = Enum(4);
}
static Flag FLAG;
void foo(int flag)
{
writefln("flag = %d", flag);
}
void main()
{
foo(FLAG.READ.value);
foo(FLAG.READ.value|FLAG.WRITE.value);
}
What I really want is something more like...
import std.stdio;
import std.string;
struct Enum
{
int value;
this(int v)
{
value = v;
}
Enum opBinary(string s:"|")(Enum rhs)
{
return Enum(value|rhs.value);
}
const string toString()
{
return format("%d", value);
}
}
struct Flag
{
Enum READ = Enum(1);
Enum WRITE = Enum(2);
Enum OTHER = Enum(4);
}
static Flag FLAG;
void foo(Enum e)
{
writefln("e = %d", e);
}
void main()
{
foo(FLAG.READ);
foo(FLAG.READ|FLAG.WRITE);
}
This is only a partial implementation, to complete it I would have to
manually define all the numeric and logical operators in my Enum struct.
What I want is for D to do all this with some syntactical sugar, eg.
enum FLAG : numeric
{
READ = 1, WRITE = 2, OTHER = 4
}
R
More information about the Digitalmars-d
mailing list