D - Singleton class or struct: how to?
- Kris (31/31) Mar 26 2004 What is the best way in D to declare a singleton? Currently I use:
- Matthew (8/39) Mar 26 2004 Just make the ctor private.
- Kris (12/13) Mar 26 2004 Did you mean this Matthew?
- Manfred Nowak (6/8) Mar 26 2004 http://www.digitalmars.com/drn-bin/wwwnews?D/21642
- Kris (3/11) Mar 26 2004 Cheers Manfred!
- Matthew (6/16) Mar 30 2004 Yep.
- Kris (4/22) Mar 31 2004 Thanks.
- Stephan Wienczny (14/20) Mar 26 2004 Use a module:
- Kris (6/26) Mar 26 2004 Cool. Thanks Stephan.
- J Anderson (4/9) Mar 26 2004 Surround it in structs and make everything static.
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
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 haveaneffect. Ideas? Comments? - Kris
Mar 26 2004
"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
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.http://www.digitalmars.com/drn-bin/wwwnews?D/21642 == <bu0da8$1qic$1 digitaldaemon.com> So long!
Mar 26 2004
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.http://www.digitalmars.com/drn-bin/wwwnews?D/21642 == <bu0da8$1qic$1 digitaldaemon.com> So long!
Mar 26 2004
"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...Yep.Just make the ctor private.Did you mean this Matthew? class Foo { private this(){} }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
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...be"Matthew" <matthew stlsoft.org> wrote in message news:c422t8$9ji$1 digitaldaemon.com...Yep.Just make the ctor private.Did you mean this Matthew? class Foo { private this(){} }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 likelyfixed in the next release (as most do).
Mar 31 2004
Kris wrote:Ideas? Comments? - KrisUse a module: //Your singleton module bar; void func() { } usage: module xyz; void another_func() { bar.func(); } Stephan
Mar 26 2004
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? - KrisUse a module: //Your singleton module bar; void func() { } usage: module xyz; void another_func() { bar.func(); } Stephan
Mar 26 2004
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? - KrisSurround it in structs and make everything static. -- -Anderson: http://badmama.com.au/~anderson/
Mar 26 2004