D has different syntax for static struct initializers and struct literals. several threads on the NG has been about this and there are enhancement requests on this tracker as well...
dmd currently accepts the invalid code:
module structinitbug;
struct S {
void* ptr;
}
void main() {
S s = {null};
}
it seems that there are several issues surrounding initializers lately...
I found this due to it actually being used in Tango (xml.Document) and LLVMDC not handling it properly.
Comment #1 by bugzilla — 2008-10-02T04:02:49Z
Static initializers are allowed for initializing non-statics. It is not a bug.
Comment #2 by tomas — 2008-10-02T05:08:57Z
It's not documented though, reopening as a spec bug
Comment #3 by kamm-removethis — 2008-10-02T11:05:13Z
What I found surprising was that you can use static initializers to initialize non-statics with non-static data. This passes on DMD and LLVMDC:
class C
{
struct S {
C ptr;
}
S foo() {
S loc = {this};
return loc;
}
}
void main()
{
auto c = new C;
assert(c.foo().ptr is c);
}
Comment #4 by samukha — 2008-10-03T02:47:52Z
What's more surprising is that not all static initializers are allowed to initialize non-statics:
int[] a = [1, 2, 3]; // ok.
int[] b = [1 : 2, 3]; // fails. only static arrays can be initialized like this
Though it seems to be according to the specs, I don't see the reason why initializers with indexes are special cased.
And this one may be considered a bug:
void foo()
{
int[3] a = [ 1:2, 3 ]; // fails. static static arrays, only:
static int[3] a = [ 1:2, 3 ]; // ok
}
Comment #5 by bugzilla — 2008-10-03T03:39:31Z
I agree it's an inconsistent design, but it is not a bug. Static initializer syntax is different. You can enter an enhancement request to change the design.
Comment #6 by tomas — 2008-10-03T04:23:08Z
but it's still not documented !!! reopening, changing the description a bit. the spec should mention that this is allowed..
Comment #7 by tomas — 2008-10-03T04:53:08Z
also the point that Christian mentions, that the initializer need not be static is especially surprising given no documentation.