digitalmars.D - Static Initializors
- Benji Smith (33/33) Jun 21 2004 I've been writing a lot of java code recently, and I've found myself
- Hauke Duden (5/8) Jun 21 2004 My thought is that it's already in there! ;)
- Benji Smith (11/18) Jun 21 2004 Hot damn. That's exactly what I was thinking of. I'm glad to see that
- (pragma) EricAnderton at yahoo dot com (16/24) Jun 21 2004 Benji,
- Trejkaz Xaoza (15/19) Jun 21 2004 Not meaning to be annoying, but this is niggling me at the moment...
- Benji Smith (11/17) Jun 22 2004 Yeah, the code I was writing was Java. And I actually didn't know
- Matthew (6/38) Jun 22 2004 They exist in both D and Java.
I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method. It looks like this: public static void myFunciton() { if (!MyClass.initialized) { MyClass.initialize(); } ...do stuff... } public static void myOtherFunciton() { if (!MyClass.initialized) { MyClass.initialize(); } ...do stuff... } private static void initialize() { ...set up static variables, connect to resources, etc... } Much in the same way that a constructor fires on the creation of new objects, I'd like to have a compiler-recognized static initialization function, automatically called (only once) whenever any static method (or static field?) is called for the first time. Hmmmmm.....come to think of it, I've been creating classes full of static methods when I should be using a singleton object. Is there much of an argument for adding the notion of singletons into the language itself, or do you think singletons should be implemented in a library? Nevertheless, even if I should be using a singleton for the stuff I'm doing right now, what do you think of the concept of an automatically-called static initializor function? --Benji Smith
Jun 21 2004
Benji Smith wrote:Nevertheless, even if I should be using a singleton for the stuff I'm doing right now, what do you think of the concept of an automatically-called static initializor function?My thought is that it's already in there! ;) See "Static constructors" in http://www.digitalmars.com/d/class.html Hauke
Jun 21 2004
On Tue, 22 Jun 2004 01:06:20 +0200, in digitalmars.D you wrote:My thought is that it's already in there! ;) See "Static constructors" in http://www.digitalmars.com/d/class.html HaukeHot damn. That's exactly what I was thinking of. I'm glad to see that it already exists (but I don't ever remember any discussion of it here. One thing that piques my curiosity, though, is this phrase:static this() is called by the startup code before main() is calledIf that's the case, what about classes loaded from a DLL during runtime. Obviously, their static constructors can't be called before the beginning of the main() function. I'd expect all of the static initializers from all of the classes in the loaded library to be called immediately after the library was loaded. Is that the case? --Benji Smith
Jun 21 2004
In article <o6ved0li4m6i4t0m835bfhfb2ali4tbi3i 4ax.com>, Benji Smith says...One thing that piques my curiosity, though, is this phrase:Benji, That's exactly how it happpens, provided you build your .dll correctly. Take a look here, there's an example of how DllMain is composed for .dll's: http://www.digitalmars.com/d/windows.html (pipes inserted to preserve spacing) |case DLL_PROCESS_ATTACH: | gc_init(); // initialize GC | _minit(); // initialize module list | _moduleCtor(); // run module constructors | _moduleUnitTests(); // run module unit tests | break; From my own observations, the call to _moduleCtor() pretty much takes care of the module's construction needs. This includes any "static this(){}" calls as well. Happy hunting.static this() is called by the startup code before main() is calledIf that's the case, what about classes loaded from a DLL during runtime. Obviously, their static constructors can't be called before the beginning of the main() function. I'd expect all of the static initializers from all of the classes in the loaded library to be called immediately after the library was loaded. Is that the case? --Benji Smith
Jun 21 2004
Not meaning to be annoying, but this is niggling me at the moment... You're not implying that such a thing doesn't exist in Java, are you? Because obviously Java can do: static { //... } Which works the same as D's: static init() { //... } Your post doesn't make it overly clear whether the code you're writing is Java or D, though it looks like Java at first glance which is why I'm a little concerned about the way you did this. :-) TX In article <aroed09oko3967j2r0lho1n67ggf0te194 4ax.com>, Benji Smith says...I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method.
Jun 21 2004
On Tue, 22 Jun 2004 05:38:01 +0000 (UTC), Trejkaz Xaoza <trejkaz xaoza.net> wrote:Not meaning to be annoying, but this is niggling me at the moment... You're not implying that such a thing doesn't exist in Java, are you? Because obviously Java can do: static { //... }Yeah, the code I was writing was Java. And I actually didn't know about this construct. How could I possibly have missed that? I've been programming Java for two years (after several years programming perl, VB, and various web languages). Thanks for the tip. Please resume your normal non-idiotic, non-time-wasting, newsgroup activities.... Sheepishly, --BenjiSmith
Jun 22 2004
"Benji Smith" <dlanguage xxagg.com> wrote in message news:aroed09oko3967j2r0lho1n67ggf0te194 4ax.com...I've been writing a lot of java code recently, and I've found myself wanting something analagous to a constructor, but for the static data in a class. Instead, I've been littering all of my static methods with a call to a private static initialize() method. It looks like this: public static void myFunciton() { if (!MyClass.initialized) { MyClass.initialize(); } ...do stuff... } public static void myOtherFunciton() { if (!MyClass.initialized) { MyClass.initialize(); } ...do stuff... } private static void initialize() { ...set up static variables, connect to resources, etc... } Much in the same way that a constructor fires on the creation of new objects, I'd like to have a compiler-recognized static initialization function, automatically called (only once) whenever any static method (or static field?) is called for the first time.They exist in both D and Java.Hmmmmm.....come to think of it, I've been creating classes full of static methods when I should be using a singleton object. Is there much of an argument for adding the notion of singletons into the language itself, or do you think singletons should be implemented in a library? Nevertheless, even if I should be using a singleton for the stuff I'm doing right now, what do you think of the concept of an automatically-called static initializor function?IMHO, D's static initialiser functionality obviates most needs for the much abused Singleton pattern. In most cases you can, and should, simply use free functions from a module, within which reside the shared "static" data.
Jun 22 2004