www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static constructor call ordering in imported modules

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Aug 08 2010
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 08.08.2010 22:55, Andrej Mitrovic 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?
    
In text I guess - consider A static constructor uses class B that is in MB (as it can, but not the other way around). MA should have B's static constructor called, or all sorts of trouble come. -- Dmitry Olshansky
Aug 08 2010
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
Andrej Mitrovic wrote:
 But, according to TDPL, page 189, it states:
 
 "MA imports MB. Then A's static class constructors run before B's"
TDPL appears to be incorrect. The compiler is correct.
Aug 08 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/08/2010 04:13 PM, Walter Bright wrote:
 Andrej Mitrovic wrote:
 But, according to TDPL, page 189, it states:

 "MA imports MB. Then A's static class constructors run before B's"
TDPL appears to be incorrect. The compiler is correct.
Yah, it's obvious that if MA imports MB then whatever stuff is in MB better be ready to roll when MA wakes up. This should be in the errata. I finally reinstated the database (which I'd deleted by mistake) and re-entered the errata by hand. Here it is: http://www.erdani.com/tdpl/errata/ Andrei
Aug 08 2010
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
Pretend the modules are classes, they behave the same way. 

Andrej Mitrovic <andrej.mitrovich 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?
Aug 08 2010