Enumeration Type-Safety in D

Maxim Fomin maxim at maxim-fomin.ru
Thu Nov 7 07:36:10 PST 2013


On Thursday, 7 November 2013 at 13:25:40 UTC, Nordlöw wrote:
> 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?

The problem is in cast() and it will pop up in both final and 
usual switch, as well as in other places.


More information about the Digitalmars-d mailing list