www.digitalmars.com         C & C++   DMDScript  

D - module toplevel static construction

reply "Dan Liebgold" <dliebgold yahoo.com> writes:
Classes can have static constructors that get called at program start time.
Can modules have implicit static constructors so that I write toplevel
initializations, like below?

static SOMECLASS instance = new SOMECLASS(blah,blah,..);

void main (char[][] args) {
    ...
}


Currently I have to wrap it in a throw-away class and put the initialization
in a static constructor.

Dan L.
Dec 05 2003
next sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Lame, isn't it?  ;(

Sean

"Dan Liebgold" <dliebgold yahoo.com> wrote in message
news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.

Dec 05 2003
prev sibling next sibling parent davepermen <davepermen_member pathlink.com> writes:
throw-away class?

A a = null;

static this() {
a = new A;
}

int main(char[][] args) {
a.doSomething();
}


throw away class???


oh, and static ~this() exists, too:D

In article <bqpgn6$h1o$1 digitaldaemon.com>, Dan Liebgold says...
Classes can have static constructors that get called at program start time.
Can modules have implicit static constructors so that I write toplevel
initializations, like below?

static SOMECLASS instance = new SOMECLASS(blah,blah,..);

void main (char[][] args) {
    ...
}


Currently I have to wrap it in a throw-away class and put the initialization
in a static constructor.

Dan L.

Dec 05 2003
prev sibling next sibling parent reply Andy Friesen <andy ikagames.com> writes:
Dan Liebgold wrote:
 Classes can have static constructors that get called at program start time.
 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?
 
 static SOMECLASS instance = new SOMECLASS(blah,blah,..);
 
 void main (char[][] args) {
     ...
 }
 
 
 Currently I have to wrap it in a throw-away class and put the initialization
 in a static constructor.
 
 Dan L.

It's not documented anywhere, but you can do exactly that. -- andy
Dec 05 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Wait.. I think I was thinking of function bodies.  There is a non-symmetry
in D between module scope, class scope, and local scope with the way
initializers work.  If you can initialize in the declaration in one part of
the program you should be able to in other parts as well.

I believe initializing to a literal or constant expression (i.e. int a =
0; ) is ok, but an initializer that must be evaluated at runtime is not
(i.e. myclass a = new myclass; )  Walter has some rationale for this, but
coming from C++, I thought it was flimsy.  I don't like the way C++ does it
either, since you cannot guarantee order of initialization of static globals
between modules, and you cannot initialize class members (even static ones)
at point of declaration, you have to put the initializer in the constructor
or in the .cpp file, respectively.  But at least it deals with locals in a
straightforward manner.

Sean

"Andy Friesen" <andy ikagames.com> wrote in message
news:bqqan7$1mdm$1 digitaldaemon.com...
 Dan Liebgold wrote:
 Classes can have static constructors that get called at program start


 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the


 in a static constructor.

 Dan L.

It's not documented anywhere, but you can do exactly that. -- andy

Dec 05 2003
prev sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Im sorry im not sure what you mean here, you want to do static module
construction ?


static this () {
 // initalize module info here

}

thats not what you want ?  I noticed the word implicit but....

Thanks,
C

"Dan Liebgold" <dliebgold yahoo.com> wrote in message
news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.

Dec 05 2003
parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Sorry just to clarify ,

you can have _explicit_ static module constructors by including this
somewhere in your module

static this () {

//module info here

}

is that what your wanting ?

C
"Charles Sanders" <sanders-consulting comcast.net> wrote in message
news:bqqjvr$23r8$1 digitaldaemon.com...
 Im sorry im not sure what you mean here, you want to do static module
 construction ?


 static this () {
  // initalize module info here

 }

 thats not what you want ?  I noticed the word implicit but....

 Thanks,
 C

 "Dan Liebgold" <dliebgold yahoo.com> wrote in message
 news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.


Dec 05 2003
next sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
I think he wants to be able to use arbitrary initializers at module scope,
not just compile time constant expressions.

The code to do the initialization would have to be moved into the start of
the static this() routine.

Currently I believe it is an error to initialize with a runtime expression.

Sean

"Charles Sanders" <sanders-consulting comcast.net> wrote in message
news:bqqpl3$2ckt$1 digitaldaemon.com...
 Sorry just to clarify ,

 you can have _explicit_ static module constructors by including this
 somewhere in your module

 static this () {

 //module info here

 }

 is that what your wanting ?

 C
 "Charles Sanders" <sanders-consulting comcast.net> wrote in message
 news:bqqjvr$23r8$1 digitaldaemon.com...
 Im sorry im not sure what you mean here, you want to do static module
 construction ?


 static this () {
  // initalize module info here

 }

 thats not what you want ?  I noticed the word implicit but....

 Thanks,
 C

 "Dan Liebgold" <dliebgold yahoo.com> wrote in message
 news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.



Dec 05 2003
prev sibling parent reply Dan Liebgold <Dan_member pathlink.com> writes:
Yes indeed... I didn't know about the explicit module static constructors.  But
it would be nice if was also doable implicitly, like the example. It'd just more
visible that way (the initialization is done at the same time as the
declaration).

I believe the example should have been:

SOMECLASS global_instance = new SOMECLASS(blah,blah,...);


..without the static attribute, so it would be visible outside the module.
When I tried this, I got a compiler error complaining that "new
SOMECLASS(blah,blah,...)" is non-constant.

Most importantly, this seems like the best (only?) way to declare global
singletons in D.

Dan L.


In article <bqqpl3$2ckt$1 digitaldaemon.com>, Charles Sanders says...
Sorry just to clarify ,

you can have _explicit_ static module constructors by including this
somewhere in your module

static this () {

//module info here

}

is that what your wanting ?

C
"Charles Sanders" <sanders-consulting comcast.net> wrote in message
news:bqqjvr$23r8$1 digitaldaemon.com...
 Im sorry im not sure what you mean here, you want to do static module
 construction ?


 static this () {
  // initalize module info here

 }

 thats not what you want ?  I noticed the word implicit but....

 Thanks,
 C

 "Dan Liebgold" <dliebgold yahoo.com> wrote in message
 news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write toplevel
 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.



Dec 05 2003
parent "Charles Sanders" <sanders-consulting comcast.net> writes:
Oh i see what yall ( you and Sean ) mean.  I wasnt aware that was illegal.

C

"Dan Liebgold" <Dan_member pathlink.com> wrote in message
news:bqr21k$2p0i$1 digitaldaemon.com...
 Yes indeed... I didn't know about the explicit module static constructors.

 it would be nice if was also doable implicitly, like the example. It'd

 visible that way (the initialization is done at the same time as the
 declaration).

 I believe the example should have been:

 SOMECLASS global_instance = new SOMECLASS(blah,blah,...);


 ..without the static attribute, so it would be visible outside the module.
 When I tried this, I got a compiler error complaining that "new
 SOMECLASS(blah,blah,...)" is non-constant.

 Most importantly, this seems like the best (only?) way to declare global
 singletons in D.

 Dan L.


 In article <bqqpl3$2ckt$1 digitaldaemon.com>, Charles Sanders says...
Sorry just to clarify ,

you can have _explicit_ static module constructors by including this
somewhere in your module

static this () {

//module info here

}

is that what your wanting ?

C
"Charles Sanders" <sanders-consulting comcast.net> wrote in message
news:bqqjvr$23r8$1 digitaldaemon.com...
 Im sorry im not sure what you mean here, you want to do static module
 construction ?


 static this () {
  // initalize module info here

 }

 thats not what you want ?  I noticed the word implicit but....

 Thanks,
 C

 "Dan Liebgold" <dliebgold yahoo.com> wrote in message
 news:bqpgn6$h1o$1 digitaldaemon.com...
 Classes can have static constructors that get called at program start

 Can modules have implicit static constructors so that I write




 initializations, like below?

 static SOMECLASS instance = new SOMECLASS(blah,blah,..);

 void main (char[][] args) {
     ...
 }


 Currently I have to wrap it in a throw-away class and put the

 in a static constructor.

 Dan L.




Dec 05 2003