www.digitalmars.com         C & C++   DMDScript  

D - Singleton class or struct: how to?

reply "Kris" <someidiot earthlink.net> writes:
What is the best way in D to declare a singleton? Currently I use:

// the singleton type
class Bar
{
    void func();
}

// the singleton instance
struct Foo
{
   static Bar bar;

   static this()
   {
   bar = new Bar();
   }
}

// usage
void test()
{
    Foo.bar.func();
}

However, a user can happily instantiate additional instances of the
singleton simply by

void test()
{
    Foo myFoo;
}

which is not cool. Also, a user can {Foo.bar = null;} since bar is not
const. I tried using the 'final' keyword, but that doesn't appear to have an
effect.

Ideas? Comments?

- Kris
Mar 26 2004
next sibling parent reply "Matthew" <matthew stlsoft.org> writes:
Just make the ctor private.

But I tend to agree with other commentators on this NG on this subject,
which is that D's module functionality - which, btw, is deterministic,
unlike that of C++ - renders the representation of singletons as objects
rather jejune. :-)


"Kris" <someidiot earthlink.net> wrote in message
news:c420o8$65e$1 digitaldaemon.com...
 What is the best way in D to declare a singleton? Currently I use:

 // the singleton type
 class Bar
 {
     void func();
 }

 // the singleton instance
 struct Foo
 {
    static Bar bar;

    static this()
    {
    bar = new Bar();
    }
 }

 // usage
 void test()
 {
     Foo.bar.func();
 }

 However, a user can happily instantiate additional instances of the
 singleton simply by

 void test()
 {
     Foo myFoo;
 }

 which is not cool. Also, a user can {Foo.bar = null;} since bar is not
 const. I tried using the 'final' keyword, but that doesn't appear to have

 effect.

 Ideas? Comments?

 - Kris

Mar 26 2004
parent reply "Kris" <someidiot earthlink.net> writes:
"Matthew" <matthew stlsoft.org> wrote in message
news:c422t8$9ji$1 digitaldaemon.com...
 Just make the ctor private.

Did you mean this Matthew? class Foo { private this(){} } the private attribute is thoroughly ignored by the compiler ... BTW, I searched (via Outlook) for other threads on Singleton, but didn't find anything. Is there someplace to search the "older" threads? (digitalmars/d/index does not support search). Thanks;
Mar 26 2004
next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
On Fri, 26 Mar 2004 12:49:56 -0800, Kris wrote:

[...]
 BTW, I searched (via Outlook) for other threads on Singleton, but didn't
 find anything.

D/21642 == <bu0da8$1qic$1 digitaldaemon.com> So long!
Mar 26 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Cheers Manfred!


"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c42omb$1bhu$1 digitaldaemon.com...
 On Fri, 26 Mar 2004 12:49:56 -0800, Kris wrote:

 [...]
 BTW, I searched (via Outlook) for other threads on Singleton, but didn't
 find anything.

D/21642 == <bu0da8$1qic$1 digitaldaemon.com> So long!

Mar 26 2004
prev sibling parent reply "Matthew" <matthew stlsoft.org> writes:
"Kris" <someidiot earthlink.net> wrote in message
news:c424kb$c4f$1 digitaldaemon.com...
 "Matthew" <matthew stlsoft.org> wrote in message
 news:c422t8$9ji$1 digitaldaemon.com...
 Just make the ctor private.

Did you mean this Matthew? class Foo { private this(){} }

Yep.
 the private attribute is thoroughly ignored by the compiler ...

It sounds flippant, but I wouldn't worry about it. You shouldn't get confused between a language feature and a compiler bug, which will likely be fixed in the next release (as most do).
Mar 30 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Thanks.

"Matthew" <matthew stlsoft.org> wrote in message
news:c4dkp1$8hh$3 digitaldaemon.com...
 "Kris" <someidiot earthlink.net> wrote in message
 news:c424kb$c4f$1 digitaldaemon.com...
 "Matthew" <matthew stlsoft.org> wrote in message
 news:c422t8$9ji$1 digitaldaemon.com...
 Just make the ctor private.

Did you mean this Matthew? class Foo { private this(){} }

Yep.
 the private attribute is thoroughly ignored by the compiler ...

It sounds flippant, but I wouldn't worry about it. You shouldn't get confused between a language feature and a compiler bug, which will likely

 fixed in the next release (as most do).

Mar 31 2004
prev sibling parent reply Stephan Wienczny <wienczny web.de> writes:
Kris wrote:
 
 Ideas? Comments?
 
 - Kris
 
 

Use a module: //Your singleton module bar; void func() { } usage: module xyz; void another_func() { bar.func(); } Stephan
Mar 26 2004
parent reply "Kris" <someidiot earthlink.net> writes:
Cool. Thanks Stephan.

In this particular case, I'd really like to keep the extended namespace
intact, as in Foo.bar.func().  Is that possible using the module-technique?

- Kris


"Stephan Wienczny" <wienczny web.de> wrote in message
news:c42345$9uo$1 digitaldaemon.com...
 Kris wrote:
 Ideas? Comments?

 - Kris

Use a module: //Your singleton module bar; void func() { } usage: module xyz; void another_func() { bar.func(); } Stephan

Mar 26 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Kris wrote:

Cool. Thanks Stephan.

In this particular case, I'd really like to keep the extended namespace
intact, as in Foo.bar.func().  Is that possible using the module-technique?

- Kris
  

-- -Anderson: http://badmama.com.au/~anderson/
Mar 26 2004