www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Singleton

reply turpeine lut.fi writes:
It would be great, if there could be singleton class inside language. Or is
there?

singleton SFoo
{
this();
}


..

SFoo a();
SFoo b();

if (&a==&b) // this would be true
Apr 08 2005
next sibling parent Georg Wrede <georg.wrede nospam.org> writes:
turpeine lut.fi wrote:
 It would be great, if there could be singleton class inside language. Or is
 there?

Would you care to elaborate on the merits of having an in-built singleton class? Also, (in general,) for such things to become a part of the language, there would have to be reasons like: - hard to work around - brittleness of work around - major usefulness of new syntax - substantial increase of stability/clearness of written code or other weighty issues. There may be some, but you'd have to explain them. (At least to thickies like me. :-) )
Apr 08 2005
prev sibling parent Benji Smith <dlanguage xxagg.com> writes:
turpeine lut.fi wrote:
 It would be great, if there could be singleton class inside language. Or is
 there?

Here's the way I would do it, using current syntax: ################################################### class MySingleton { public static Object myObject = null; static { // perform some sort of initialization on myObject } // Private ctor prevents object from direct instantiation private MySingleton() {} public static getSingletor() { return myObject; } } ################################################### What's wrong with that? A singleton is a simple pattern (like a factory) that can be **easily** implemented, without changing the language at all. --BenjiSmith
Apr 11 2005