digitalmars.D.bugs - Module & class static-ctor invocation?
- Kris (20/20) Jan 12 2006 ~~~~~~~~~~~~~~~~~~~~~~~~~
- Sean Kelly (6/33) Jan 12 2006 Good question. The current behavior seems to follow declaration order
- Kris (7/40) Jan 12 2006 minor clarification:
- Carlos Santander (11/59) Jan 12 2006 Apparently it always is in the order they appear in the file (lexical or...
- BCS (6/44) Jan 12 2006 What if the module ctor needs to use one of it's classes? Maybe there sh...
- Sean Kelly (4/47) Jan 12 2006 Good point. So perhaps initialization should always occur in
- BCS (5/15) Jan 12 2006 That might help. But what if a class ctor needs to be run in the middle ...
- Sean Kelly (4/17) Jan 12 2006 Then I'd accuse the programmer of making bad design decisions and let
- BCS (8/43) Jan 12 2006 It seems to me that it would be an unnecessary limitation to not allow e...
- Kris (13/62) Jan 12 2006 This is all fine & good, except for one thing;
- Sean Kelly (3/15) Jan 12 2006 True enough. And it's what I'd expect to happen anyway.
- Walter Bright (5/5) Jan 15 2006 The static constructors are invoked in lexical order. This makes for a
- Thomas Kuehne (11/31) Jan 31 2006 -----BEGIN PGP SIGNED MESSAGE-----
~~~~~~~~~~~~~~~~~~~~~~~~~ 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
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
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
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...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 BernalKris 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
Sean Kelly wrote:Kris wrote: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.~~~~~~~~~~~~~~~~~~~~~~~~~ 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
BCS wrote:Sean Kelly wrote:Good point. So perhaps initialization should always occur in declaration order? This seems entirely reasonable IMO. SeanKris wrote: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.~~~~~~~~~~~~~~~~~~~~~~~~~ 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.
Jan 12 2006
Sean Kelly wrote:BCS wrote: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?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
BCS wrote:Sean Kelly wrote:Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) SeanBCS wrote: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?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.
Jan 12 2006
Sean Kelly wrote:BCS wrote: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:Sean Kelly wrote:Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) SeanBCS wrote: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?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.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
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: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:Sean Kelly wrote:Then I'd accuse the programmer of making bad design decisions and let him worry about fixing them ;-) SeanBCS wrote: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?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.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
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
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
-----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