Enumeration Type-Safety in D
Namespace
rswhite4 at googlemail.com
Thu Nov 7 05:34:19 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?
Use std.conv : to:
e = to!E(3);
results in:
std.conv.ConvException@/opt/compilers/dmd2/include/std/conv.d(1854):
Value (3) does not match any member value of enum 'E'
More information about the Digitalmars-d
mailing list