Singleton Pattern

Jonathan M Davis jmdavisProg at
Wed Jan 11 17:04:23 PST 2012

On Thursday, January 12, 2012 00:55:14 Thomas Mader wrote:
> On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg at> wrote:
> > The same way that you would in C++ or Java, except that unlike you have
> static
> The most simple and elegant way in java is to use an enum. Is there nothing
> comparable in D?

No, because enums must be known at compile time. This avoids the ordering 
issues that you get in languages like C++ or Java when you directly initialize 
static variables like that. So, to have an enum which is a class, you need to 
be able to construct a class at compile time and have it persist into the 
runtime of the program. And while you _can_ now use classes with CTFE, they 
cannot persist beyond CTFE (though that may be possible eventually). Any class 
which is used at runtime must be constructed at runtime. So, either you need 
to use a static constructor, or you need to initialize the singleton lazily.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list