On Monday, 16 July 2012 at 21:00:00 UTC, cal wrote:
On Monday, 16 July 2012 at 20:22:12 UTC, Era Scarecrow wrote:
You can convert a enum to an int, but not the other way around.
[...]
MyEnumType z = cast(MyEnumType) 100; //Error: an int is not an enum! (Even if it's a valid match)

I do this all the time....

enum E
{
 A = 100,
 B = 200
}

E e = cast(E) 100 ; // works fine...

Maybe I misunderstood?

The problem is that here int is not _implicitly_ convertible to the enum. For example, if you write your own template without any constraints:

T to(T, S)(S i)
{
    return cast(T)i;
}

then:
E e = to!E(100); // works, but is not safe

actually ignore that bit about implicit conversion, that's probably not "the problem"


Reply via email to