order of static constructor execution

Fawzi Mohamed fawzi at gmx.ch
Fri Mar 12 13:55:04 PST 2010


On 12-mar-10, at 19:17, Walter Bright wrote:

> Doing lazy initialization certainly works, but it would require all  
> static member access to go through a check for initialization,  
> first. The cost of this check persists for a statically compiled  
> language; for a JITted language like C# the access can be rewritten  
> to remove the check.

well if one tracks what gets initialized and what is accessed then one  
could associate a lazy constructor with each static value, and call  
the static initializer explicitly before access in the static methods,  
so that the cost is paid only during startup.
But that is quite some work, because static initializers in D are so  
flexible, and so tracking what they initialize and what they access is  
some work (even if theoretically the compiler can know it).
Also invalid initializations could be catched (but only at runtime)  
having 3 initialization states: on initialized, during initialization,  
initialized.
I still think that something like my proposal @dependOnly(modules) for  
static initializers is a valid alternative.

Fawzi



More information about the Digitalmars-d mailing list