D - Singleton class or struct: how to?
- "Kris" <someidiot earthlink.net> Mar 26 2004
- "Matthew" <matthew stlsoft.org> Mar 26 2004
- "Kris" <someidiot earthlink.net> Mar 26 2004
- Manfred Nowak <svv1999 hotmail.com> Mar 26 2004
- "Kris" <someidiot earthlink.dot.dot.dot.net> Mar 26 2004
- "Matthew" <matthew stlsoft.org> Mar 30 2004
- "Kris" <someidiot earthlink.dot.dot.dot.net> Mar 31 2004
- Stephan Wienczny <wienczny web.de> Mar 26 2004
- "Kris" <someidiot earthlink.net> Mar 26 2004
- J Anderson <REMOVEanderson badmama.com.au> Mar 26 2004
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 have
effect. 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.
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.
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...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
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
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
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
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









"Kris" <someidiot earthlink.dot.dot.dot.net> 