Singleton in Action?

Jacob Carlborg doob at me.com
Sun Feb 3 18:53:10 UTC 2019


On 2019-02-02 17:56, Ron Tarrant wrote:
> Hi guys,
> 
> I ran into another snag this morning while trying to implement a 
> singleton. I found all kinds of examples of singleton definitions, but 
> nothing about how to put them into practice.
> 
> Can someone show me a code example for how one would actually use a 
> singleton pattern in D? When I did the same thing in PHP, it took me 
> forever to wrap my brain around it, so I'm hoping to get there a little 
> faster this time.

You don't need to make it so complicated. Here's a simpler example:

class DSingleton
{
     private __gshared auto instance_ = new DSingleton;

     private this() // private to make sure no one else can create an 
instance
     {

     }

     static DSingleton instance()
     {
         return instance_;
     }
}

void main()
{
     writeln(DSingleton.instance);
}


-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list