Comment #0 by andrej.mitrovich — 2011-08-27T16:41:33Z
enum Length;
void main()
{
int[Length] a;
auto b = a;
}
Notice I forgot to declare what Length is, I might have wanted to declare "enum Length = 5".
This creates a whole host of errors depending on how you use the code, e.g.:
void main()
{
int[Length] a;
auto b = a;
writeln(b);
}
Error: enum test.Length is forward referenced when looking for 'stringof'
Or:
import std.stdio;
enum Length;
void main()
{
int[Length] a;
auto b = a;
writeln(b.length);
}
Error: Assertion failure: 'impl' on line 4080 in file 'mtype.c'
abnormal program termination
Or:
import std.stdio;
enum Length;
void main()
{
writeln(Length);
}
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1483): Error: forward reference of Length.init
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1495): Error: forward reference of Length.init
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1508): Error: forward reference of Length.init
Anyway, the first example should not compile.
Comment #1 by yebblies — 2012-01-30T21:59:16Z
enum Length; is a forward declaration to an enum type defined elsewhere.
eg.
--- x.d ---
enum Length
{
... members ...
}
--- x.di ---
enum Length;
Not sure this is the greatest thing to allow, but it is correct according to the spec. Please open an enhancement request to disallow forward declarations of enums is you think this is desirable.
Comment #2 by andrej.mitrovich — 2012-01-30T22:19:30Z
(In reply to comment #1)
> --- x.di ---
> enum Length;
That's not generated by the compiler (at least not 2.057, I don't know about 2.058 HEAD). But how does it make sense to hide enum members anyway? I've never seen D code that uses a forward enum declaration.
Comment #3 by yebblies — 2012-01-30T22:25:22Z
(In reply to comment #2)
> That's not generated by the compiler (at least not 2.057, I don't know about
> 2.058 HEAD).
It doesn't get generated by the compiler, but you can still do it.
> But how does it make sense to hide enum members anyway?
An enum declaration creates its own type, so in the same kind of places you'd want to pass around an opaque struct pointer.
> I've never seen D code that uses a forward enum declaration.
Neither have I, but I've seen plenty of C/C++ source that does it. eg dmd
Comment #4 by andrej.mitrovich — 2012-01-30T22:38:43Z
(In reply to comment #3)
> An enum declaration creates its own type, so in the same kind of places you'd
> want to pass around an opaque struct pointer.
Like "struct Foo;", I see. I've seen opaque structs before but never opaque enums. :)