www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Module & class static-ctor invocation?

reply "Kris" <fu bar.com> writes:
~~~~~~~~~~~~~~~~~~~~~~~~~
private import std.stdio;

class Foo
{
        static this() {printf("class static ctor\n");}
        static ~this() {printf("class static dtor\n");}
}

static this() {printf("static ctor\n");}
static ~this() {printf("static dtor\n");}

void main()
{
}

~~~~~~~~~~~~~~~~~~~~~~~~~~
emits the following:

class static ctor
static ctor
class static dtor
static dtor


Surely the first two should be reversed? That is, the static ctor should 
invoke any static-class-ctors *after* its own body? 
Jan 12 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Kris wrote:
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;
 
 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }
 
 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}
 
 void main()
 {
 }
 
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:
 
 class static ctor
 static ctor
 class static dtor
 static dtor
 
 
 Surely the first two should be reversed? That is, the static ctor should 
 invoke any static-class-ctors *after* its own body? 
Good question. The current behavior seems to follow declaration order which might be reasonable so long as it's documented (in fact I think it is?). But I agree that it's counter-intuitive for the module ctor to not always be first. Sean
Jan 12 2006
next sibling parent reply "Kris" <fu bar.com> writes:
minor clarification:

The *class* static ctors are invoked in some undefined order, but the 
*module* static-ctor is currently always executed last. This is because it's 
the module static-ctor that invokes the class static-ctors. That "preamble" 
code should perhaps be "postamble" instead :)



"Sean Kelly" <sean f4.ca> wrote in message 
news:dq6g50$19l0$1 digitaldaemon.com...
 Kris wrote:
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;

 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }

 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}

 void main()
 {
 }

 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:

 class static ctor
 static ctor
 class static dtor
 static dtor


 Surely the first two should be reversed? That is, the static ctor should 
 invoke any static-class-ctors *after* its own body?
Good question. The current behavior seems to follow declaration order which might be reasonable so long as it's documented (in fact I think it is?). But I agree that it's counter-intuitive for the module ctor to not always be first. Sean
Jan 12 2006
parent Carlos Santander <csantander619 gmail.com> writes:
Kris escribió:
 minor clarification:
 
 The *class* static ctors are invoked in some undefined order, but the 
 *module* static-ctor is currently always executed last. This is because it's 
 the module static-ctor that invokes the class static-ctors. That "preamble" 
 code should perhaps be "postamble" instead :)
 
 
 
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:dq6g50$19l0$1 digitaldaemon.com...
 Kris wrote:
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;

 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }

 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}

 void main()
 {
 }

 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:

 class static ctor
 static ctor
 class static dtor
 static dtor


 Surely the first two should be reversed? That is, the static ctor should 
 invoke any static-class-ctors *after* its own body?
Good question. The current behavior seems to follow declaration order which might be reasonable so long as it's documented (in fact I think it is?). But I agree that it's counter-intuitive for the module ctor to not always be first. Sean
Apparently it always is in the order they appear in the file (lexical order?) Put as many classes with static ctors as you want, and mix them with module static ctors, and see how they're executed. In another related bug, the compiler accepts this: module a; static this() {} static this() {} But the linker fails. -- Carlos Santander Bernal
Jan 12 2006
prev sibling parent reply BCS <BCS_member pathlink.com> writes:
Sean Kelly wrote:
 Kris wrote:
 
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;

 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }

 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}

 void main()
 {
 }

 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:

 class static ctor
 static ctor
 class static dtor
 static dtor


 Surely the first two should be reversed? That is, the static ctor 
 should invoke any static-class-ctors *after* its own body? 
Good question. The current behavior seems to follow declaration order which might be reasonable so long as it's documented (in fact I think it is?). But I agree that it's counter-intuitive for the module ctor to not always be first. Sean
What if the module ctor needs to use one of it's classes? Maybe there should be a (documented) default and a syntax to explicitly call the class ctor's in whatever order is needed. This would be needed if one class is needed by the module ctor and another class must be done after the module ctor.
Jan 12 2006
parent reply Sean Kelly <sean f4.ca> writes:
BCS wrote:
 Sean Kelly wrote:
 Kris wrote:

 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;

 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }

 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}

 void main()
 {
 }

 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:

 class static ctor
 static ctor
 class static dtor
 static dtor


 Surely the first two should be reversed? That is, the static ctor 
 should invoke any static-class-ctors *after* its own body? 
Good question. The current behavior seems to follow declaration order which might be reasonable so long as it's documented (in fact I think it is?). But I agree that it's counter-intuitive for the module ctor to not always be first.
What if the module ctor needs to use one of it's classes? Maybe there should be a (documented) default and a syntax to explicitly call the class ctor's in whatever order is needed. This would be needed if one class is needed by the module ctor and another class must be done after the module ctor.
Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO. Sean
Jan 12 2006
parent reply BCS <BCS_member pathlink.com> writes:
Sean Kelly wrote:
 BCS wrote:
 This would be needed if one class is needed by the module ctor and 
 another class must be done after the module ctor.
Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO. Sean
That might help. But what if a class ctor needs to be run in the middle of the module ctor, e.i. the class ctor depends on part of the module ctor but the module ctor also needs the class ctor to have run before it can do the rest of it's thing?
Jan 12 2006
parent reply Sean Kelly <sean f4.ca> writes:
BCS wrote:
 Sean Kelly wrote:
 BCS wrote:
 This would be needed if one class is needed by the module ctor and 
 another class must be done after the module ctor.
Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO.
That might help. But what if a class ctor needs to be run in the middle of the module ctor, e.i. the class ctor depends on part of the module ctor but the module ctor also needs the class ctor to have run before it can do the rest of it's thing?
Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) Sean
Jan 12 2006
parent reply BCS <BCS_member pathlink.com> writes:
Sean Kelly wrote:
 BCS wrote:
 
 Sean Kelly wrote:

 BCS wrote:

 This would be needed if one class is needed by the module ctor and 
 another class must be done after the module ctor.
Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO.
That might help. But what if a class ctor needs to be run in the middle of the module ctor, e.i. the class ctor depends on part of the module ctor but the module ctor also needs the class ctor to have run before it can do the rest of it's thing?
Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) Sean
It seems to me that it would be an unnecessary limitation to not allow explicit ordering of static ctor's. Yes you probably can get around the necessary of it, but it just seems messy to me. OTOH, if this is supposed to work this would have the same effect: Carlos Santander wrote:
 Kris escribió:

 Apparently it always is in the order they appear in the file (lexical
 order?) Put as many classes with static ctors as you want, and mix them
 with module static ctors, and see how they're executed.

 In another related bug, the compiler accepts this:

 module a;
 static this() {}
 static this() {}

 But the linker fails.
But it still has the oddity that the ordering of code (functions, classes, etc) makes a difference. This just seems like a place for VARY subtitle bugs to crop up.
Jan 12 2006
parent reply "Kris" <fu bar.com> writes:
This is all fine & good, except for one thing;

Walter is rather unlikely to provide direct, explicit control over static 
ctors. It's just not that important right now. All I'm looking for is a 
qualification as to how the module static-ctor should behave with respect to 
contained class static-ctors ~ isn't that a much simpler, and 
straightforward, request?

At this point, the static module-ctor itself invokes any contained static 
class-ctors, *before* its own body is executed. This could just as easily be 
done afterwards, such that the management would at least follows a pattern 
we're all familiar with :: first in, last out.

- Kris


"BCS" <BCS_member pathlink.com> wrote in message 
news:dq6r2a$1evk$1 digitaldaemon.com...
 Sean Kelly wrote:
 BCS wrote:

 Sean Kelly wrote:

 BCS wrote:

 This would be needed if one class is needed by the module ctor and 
 another class must be done after the module ctor.
Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO.
That might help. But what if a class ctor needs to be run in the middle of the module ctor, e.i. the class ctor depends on part of the module ctor but the module ctor also needs the class ctor to have run before it can do the rest of it's thing?
Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) Sean
It seems to me that it would be an unnecessary limitation to not allow explicit ordering of static ctor's. Yes you probably can get around the necessary of it, but it just seems messy to me. OTOH, if this is supposed to work this would have the same effect: Carlos Santander wrote:
 Kris escribió:

 Apparently it always is in the order they appear in the file (lexical
 order?) Put as many classes with static ctors as you want, and mix them
 with module static ctors, and see how they're executed.

 In another related bug, the compiler accepts this:

 module a;
 static this() {}
 static this() {}

 But the linker fails.
But it still has the oddity that the ordering of code (functions, classes, etc) makes a difference. This just seems like a place for VARY subtitle bugs to crop up.
Jan 12 2006
parent Sean Kelly <sean f4.ca> writes:
Kris wrote:
 This is all fine & good, except for one thing;
 
 Walter is rather unlikely to provide direct, explicit control over static 
 ctors. It's just not that important right now. All I'm looking for is a 
 qualification as to how the module static-ctor should behave with respect to 
 contained class static-ctors ~ isn't that a much simpler, and 
 straightforward, request?
 
 At this point, the static module-ctor itself invokes any contained static 
 class-ctors, *before* its own body is executed. This could just as easily be 
 done afterwards, such that the management would at least follows a pattern 
 we're all familiar with :: first in, last out.
True enough. And it's what I'd expect to happen anyway. Sean
Jan 12 2006
prev sibling next sibling parent "Walter Bright" <newshound digitalmars.com> writes:
The static constructors are invoked in lexical order. This makes for a 
simple rule, and if the programmer needs a different order, it can be 
easilly accomplished by having the static constructors call other functions.

The bug here is that the static destructors should be called in the reverse 
order. 
Jan 15 2006
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kris schrieb am 2006-01-12:
 ~~~~~~~~~~~~~~~~~~~~~~~~~
 private import std.stdio;

 class Foo
 {
         static this() {printf("class static ctor\n");}
         static ~this() {printf("class static dtor\n");}
 }

 static this() {printf("static ctor\n");}
 static ~this() {printf("static dtor\n");}

 void main()
 {
 }

 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 emits the following:

 class static ctor
 static ctor
 class static dtor
 static dtor


 Surely the first two should be reversed? That is, the static ctor should 
 invoke any static-class-ctors *after* its own body? 
Added to DStress as http://dstress.kuehne.cn/norun/d/destructor_06.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFD30Al3w+/yD4P9tIRAh8PAKDErW9BFMbAhaOrf9piBbDupe2AtgCeJ9B1 gDj30oV0N7TYKGLfcK1D5UY= =A5Np -----END PGP SIGNATURE-----
Jan 31 2006