Static constructor call ordering in imported modules

Sean Kelly sean at invisibleduck.org
Sun Aug 8 16:42:39 PDT 2010


Pretend the modules are classes, they behave the same way. 

Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> Here's an example from TDPL (with writeln's) with two modules:
> 
> -------------------------------------
> module MA;
> 
> import std.stdio;
> import MB;
> 
> class A
> {
>     static this()
>     {
>         writeln("A's constructor called.");
>     }
> }
> 
> void main()
> {
> }
> -------------------------------------
> 
> -------------------------------------
> module MB;
> 
> import std.stdio;
> 
> class B
> {
>     static this()
>     {
>         writeln("B's constructor called.");
>     }
> }
> -------------------------------------
> 
> Compiling and running this examples gives this output:
> B's constructor called.
> A's constructor called.
> 
> 
> But, according to TDPL, page 189, it states:
> 
> "MA imports MB. Then A's static class constructors run before B's"
> 
> I've tried using a driver module which imports MA and then MB, but as
> long as MA itself imports MB then I still get the same output. Error
> in text / in DMD?


More information about the Digitalmars-d mailing list