Jump to page: 1 2
Thread overview
Module & class static-ctor invocation?
Jan 12, 2006
Kris
Jan 12, 2006
Sean Kelly
Jan 12, 2006
Kris
Jan 12, 2006
Carlos Santander
Jan 12, 2006
BCS
Jan 12, 2006
Sean Kelly
Jan 12, 2006
BCS
Jan 12, 2006
Sean Kelly
Jan 13, 2006
BCS
Jan 13, 2006
Kris
Jan 13, 2006
Sean Kelly
Jan 15, 2006
Walter Bright
Jan 31, 2006
Thomas Kuehne
January 12, 2006
~~~~~~~~~~~~~~~~~~~~~~~~~
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?


January 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
January 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


January 12, 2006
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.
January 12, 2006
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
January 12, 2006
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?
January 12, 2006
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
January 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...
>> 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
January 13, 2006
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.
January 13, 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:
>>
>>> 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.


« First   ‹ Prev
1 2