immutable singleton pattern with static opCall

2010-06-26 Thread Justin Johansson
The following declaration implements the classic singleton design pattern and uses D's static opCall feature to make for brevity of use, that is to say, you can access the singleton instance by simply writing Foo() rather than Foo.instance. Furthermore, the use of private on the static instance f

Re: immutable singleton pattern with static opCall

2010-06-26 Thread Michal Minich
On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: > immutable class Foo > { > static private Foo instance; > > static this() { // line 9 >instance = new Foo; > } > > static Foo opCall() { // line 13 >return instance; > }

Re: immutable singleton pattern with static opCall

2010-06-26 Thread Justin Johansson
Michal Minich wrote: On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: immutable class Foo { static private Foo instance; static this() { // line 9 instance = new Foo; } static Foo opCall() { // line 13 return instance;

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Steven Schveighoffer
On Sat, 26 Jun 2010 20:19:44 -0400, Michal Minich wrote: On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: immutable class Foo { static private Foo instance; static this() { // line 9 instance = new Foo; } static Foo opCall() {

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Justin Johansson
Steven Schveighoffer wrote: On Sat, 26 Jun 2010 20:19:44 -0400, Michal Minich wrote: On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: immutable class Foo { static private Foo instance; static this() {// line 9 instance = new Foo; } static Foo

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Steven Schveighoffer
On Mon, 28 Jun 2010 08:07:40 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: On Sat, 26 Jun 2010 20:19:44 -0400, Michal Minich wrote: On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: immutable class Foo { static private Foo instance; static this() {

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Justin Johansson
Steven Schveighoffer wrote: On Mon, 28 Jun 2010 08:07:40 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: btw. The reason I marked the static instance member as private was simply to enforce stylist use of Foo() rather than Foo.instance Yuck Foo().xyz :) But, whatever floats your

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Steven Schveighoffer
On Mon, 28 Jun 2010 08:40:36 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: On Mon, 28 Jun 2010 08:07:40 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: To go one step further, if you want it to truly be a singleton type, you should mark the constructor private.

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Justin Johansson
Steven Schveighoffer wrote: On Mon, 28 Jun 2010 08:40:36 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: On Mon, 28 Jun 2010 08:07:40 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: To go one step further, if you want it to truly be a singleton type, you should mark

Re: immutable singleton pattern with static opCall

2010-06-28 Thread Justin Johansson
Steven Schveighoffer wrote: On Mon, 28 Jun 2010 08:07:40 -0400, Justin Johansson wrote: Steven Schveighoffer wrote: On Sat, 26 Jun 2010 20:19:44 -0400, Michal Minich wrote: On Sun, 27 Jun 2010 09:36:04 +0930, Justin Johansson wrote: immutable class Foo { static private Foo instance;