D - module toplevel static construction
- Dan Liebgold (10/10) Dec 05 2003 Classes can have static constructors that get called at program start ti...
- Sean L. Palmer (6/16) Dec 05 2003 Lame, isn't it? ;(
- davepermen (11/21) Dec 05 2003 throw-away class?
- Andy Friesen (3/18) Dec 05 2003 It's not documented anywhere, but you can do exactly that.
- Sean L. Palmer (18/36) Dec 05 2003 Wait.. I think I was thinking of function bodies. There is a non-symmet...
- Charles Sanders (12/22) Dec 05 2003 Im sorry im not sure what you mean here, you want to do static module
- Charles Sanders (10/40) Dec 05 2003 Sorry just to clarify ,
- Sean L. Palmer (8/56) Dec 05 2003 I think he wants to be able to use arbitrary initializers at module scop...
- Dan Liebgold (13/61) Dec 05 2003 Yes indeed... I didn't know about the explicit module static constructor...
- Charles Sanders (7/77) Dec 05 2003 Oh i see what yall ( you and Sean ) mean. I wasnt aware that was illega...
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
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 starttime.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 theinitializationin a static constructor. Dan L.
Dec 05 2003
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
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
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:time.Classes can have static constructors that get called at program startinitializationCan 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 thein a static constructor. Dan L.It's not documented anywhere, but you can do exactly that. -- andy
Dec 05 2003
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 starttime.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 theinitializationin a static constructor. Dan L.
Dec 05 2003
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 starttime.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 theinitializationin a static constructor. Dan L.
Dec 05 2003
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 starttime.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 theinitializationin a static constructor. Dan L.
Dec 05 2003
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 starttime.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 theinitializationin a static constructor. Dan L.
Dec 05 2003
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.Butit would be nice if was also doable implicitly, like the example. It'djust morevisible 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...toplevelSorry 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 starttime.Can modules have implicit static constructors so that I writeinitializations, 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 theinitializationin a static constructor. Dan L.
Dec 05 2003