Enumeration Type-Safety in D

"Nordlöw" per.nordlow at gmail.com
Thu Nov 7 05:25:38 PST 2013


What is the state and plans on type-safety of enums in D?

I expected

import std.stdio: writeln;

void main(string args[]) {
     enum E {x, y, z}
     E e;
     writeln(e);
     e = cast(E)3;
     writeln(e);
}

to fail to compile because of D's otherwise strong static 
type/range checking or at least give an RangeException when run.

To my surprise, it instead prints

cast(E)3

Is this really the preferred default behaviour for the majority 
of use cases?


More information about the Digitalmars-d mailing list