Thread overview
Singleton class or struct: how to?
Mar 26, 2004
Kris
Mar 26, 2004
Matthew
Mar 26, 2004
Kris
Mar 27, 2004
Manfred Nowak
Mar 27, 2004
Kris
Mar 31, 2004
Matthew
Mar 31, 2004
Kris
Mar 26, 2004
Stephan Wienczny
Mar 26, 2004
Kris
Mar 27, 2004
J Anderson
March 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


March 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
an
> effect.
>
> Ideas? Comments?
>
> - Kris
>
>


March 26, 2004
Kris wrote:
> 
> Ideas? Comments?
> 
> - Kris
> 
> 

Use a module:

//Your singleton
module bar;

void func()
{

}

usage:

module xyz;

void another_func()
{
	bar.func();
}

Stephan

March 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;


March 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
>


March 27, 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!
March 27, 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
>  
>
Surround it in structs and make everything static.

-- 
-Anderson: http://badmama.com.au/~anderson/
March 27, 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!


March 31, 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).




March 31, 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
be
> fixed in the next release (as most do).
>
>
>
>