Singleton in Action?

Russel Winder russel at
Sat Feb 2 17:30:05 UTC 2019

On Sat, 2019-02-02 at 16:56 +0000, Ron Tarrant via Digitalmars-d-learn wrote:
> Hi guys,

and gals.

> I ran into another snag this morning while trying to implement a 
> singleton. I found all kinds of examples of singleton 
> definitions, but nothing about how to put them into practice.

General, these days, as far as I know, Singleton is considered one of the
ultimate anti-patterns of software development. There are many positive
reasons for this very negative view of Singleton especially if there is any
hint of concurrency or parallelism involved. Also Singletons tend to deny
sensible testing.

> Can someone show me a code example for how one would actually use 
> a singleton pattern in D? When I did the same thing in PHP, it 
> took me forever to wrap my brain around it, so I'm hoping to get 
> there a little faster this time.

I'd suggest just not going here. Let's not have Singleton in D code.

Alternatives include "Parameterize From Above" which I believe Kevlin Henney
pushes strongly, and indeed he may be the source of the "pattern". (I believe
it is a design not a pattern using the proper definition of pattern.)

If the resource really is "singleton" then creating an actor or agent is now
the usual solution in languages such as D, Rust, Go, even C++.

> So, my big question is, do I instantiate like this:
> DSingleton singleton = new DSingleton;

This seems wrong.

> Or like this:
> DSingleton singleton = singleton.get();

getting seems like the one true way, if you really have to do this.

Can I indent that Singleton not be part of the canon of D programming.

> And subsequent calls would be...? The same? Using get() only?
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the Digitalmars-d-learn mailing list