Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 07, 2013 Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
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? |
November 07, 2013 Re: Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Thursday, 7 November 2013 at 13:25:40 UTC, Nordlöw wrote:
> Is this really the preferred default behaviour for the majority of use cases?
cast usage == breaking type system. Don't expect any help from it if you are using explicit casts.
|
November 07, 2013 Re: Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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' |
November 07, 2013 Re: Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw: > to fail to compile because of D's otherwise strong static type/range checking or at least give an RangeException when run. cast() is designed to punch holes in the type system, so your code doesn't show problems in D. On the other hand see: https://d.puremagic.com/issues/show_bug.cgi?id=3999 Bye, bearophile |
November 07, 2013 Re: Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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.
|
November 07, 2013 Re: Enumeration Type-Safety in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Thursday, November 07, 2013 14:25:38 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?
If you're casting, then I think that it's perfectly reasonable that this sort of thing can happen. The problem is when it happens without casting. e.g.
enum E : string { a = "hello", b = "goodbye" }
void main()
{
E foo = E.a;
foo ~= " world";
assert(foo == "hello world");
}
or
enum E : int { a = 1, b = 2 }
void main()
{
E foo = E.a | E.b;
assert(foo == 3);
}
IMHO, it should not be legal to construct invalid enums without casting, but unfortunately, right now, it very much is. For the most part, you can do operations on enums that are completely valid for their base type, and the result ends up being the enum type instead of the base type like it should. I don't know what the odds are of getting this fixed, but I think that it should be.
However, even it were properly enforced that operations done on enum values would either be guaranteed to result in a valid enum value or would result in the base type instead of the enum type, I'd still expect casting to get around that as casting is a blunt instrument which forces the issue. As others have pointed out, you should use std.conv.to if you want the conversion to be checked for validity.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation