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

Reply via email to