Static constructors inconsistency

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 27 06:31:38 PDT 2014


spec:

> Although, yeah, it would be nice if the compiler emitted some 
> kind of warning pointing this (if it's at all possible). What 
> got me confused was indeed the disparity of results from 
> changing small things.


D doesn't like warnings, but it could be an error. This is 
minimized code:


struct Foo {
     static this() {
         Bar b;
         int x = b.data[0];
     }
}
struct Bar {
     static int[] data;
     static this() {
         data.length++;
     }
}
void main() {}


A similar example:

struct Foo {
     static this() {
         Bar b;
         int x = b.data[0];
     }
}
struct Bar {
     immutable static int[] data;
     static this() {
         data.length++;
     }
}
void main() {}



Is it possible and a good idea to raise a compilation error in 
such cases where code tries to use Bar static fields before bar 
static this() has run? (But perhaps a more fine-grained error is 
needed, that tracks single fields, to allow more flexible code of 
mutually initializing constructors).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list