February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:
> I found here an example:
> https://rosettacode.org/wiki/Singleton#D
>
> Kind regards
> Andre
Thanks, Andre. Exactly what I was hoping for.
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:
> https://rosettacode.org/wiki/Singleton#D
Do you know if this is for a current version of D? The compiler is choking on the import statements, complaining that it can't read std/thread.d and std/c/time.d
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | On Saturday, 2 February 2019 at 20:30:15 UTC, Neia Neutuladh wrote:
> And consider putting the class in its own source file.
Yes, by all means.
Speaking of which...
Considering the nature of a singleton such the one in the top post, I can't see it being possible to use one as a base class from which to derive other classes... speaking from a theoretical POV.
Thanks, guys, for all the input.
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ron Tarrant | On Sunday, 3 February 2019 at 09:46:20 UTC, Ron Tarrant wrote: > On Saturday, 2 February 2019 at 20:30:15 UTC, Neia Neutuladh wrote: > >> And consider putting the class in its own source file. > > Yes, by all means. > > Speaking of which... > > Considering the nature of a singleton such the one in the top post, I can't see it being possible to use one as a base class from which to derive other classes... speaking from a theoretical POV. > > Thanks, guys, for all the input. Isn't deriving a singleton even eviler as having one? ;) https://codeblog.jonskeet.uk/2006/01/19/singleton-inheritance/ |
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ron Tarrant | On Sunday, February 3, 2019 2:41:48 AM MST Ron Tarrant via Digitalmars-d- learn wrote:
> On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:
> > https://rosettacode.org/wiki/Singleton#D
>
> Do you know if this is for a current version of D? The compiler is choking on the import statements, complaining that it can't read std/thread.d and std/c/time.d
I don't recall std.thread ever existing, and std.c.time hasn't been around for a while. Thread is in core.thread, and all of the C bindings for standard C and OS APIs are supposed to be in druntime. So, the equivalent to C's time.h would be core.stdc.time.
- Jonathan M Davis
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Sunday, 3 February 2019 at 11:17:38 UTC, Jonathan M Davis wrote:
> I don't recall std.thread ever existing, and std.c.time hasn't been around for a while. Thread is in core.thread, and all of the C bindings for standard C and OS APIs are supposed to be in druntime. So, the equivalent to C's time.h would be core.stdc.time.
>
> - Jonathan M Davis
Thanks for clearing that up, Jonathan.
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Sunday, 3 February 2019 at 10:28:51 UTC, Alex wrote:
> Isn't deriving a singleton even eviler as having one? ;)
Perhaps this was meant as rhetoric, but I think you may be right.
This morning I was Googling "singleton replacement" and someone on another forum said Factory would do the job. Anyone have thoughts on that?
(Personally, I don't see it, but I'm willing to update my position based on new evidence.)
|
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ron Tarrant Attachments:
| On Sun, 2019-02-03 at 14:42 +0000, Ron Tarrant via Digitalmars-d-learn wrote: > On Sunday, 3 February 2019 at 10:28:51 UTC, Alex wrote: > > > Isn't deriving a singleton even eviler as having one? ;) > > Perhaps this was meant as rhetoric, but I think you may be right. > > This morning I was Googling "singleton replacement" and someone on another forum said Factory would do the job. Anyone have thoughts on that? > > (Personally, I don't see it, but I'm willing to update my position based on new evidence.) There is a lot of good stuff (both positive and negative) on Singleton here, but there is also a bit of prejudice and bigotry. Many of the links are worth looking through. https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons The good use case for Singleton is very rare, most people use them wrongly. It is all about eschewing all global state except when it is the one and only way of doing the design correctly. But then you have to use it correctly. I currently have two Singletons in all my code, one I am trying to get rid of, the other is fair enough. I think, but I'd still like to get rid of it. -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ron Tarrant | 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 |
February 03, 2019 Re: Singleton in Action? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ron Tarrant | On Saturday, 2 February 2019 at 16:56:45 UTC, 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.
I strongly suggest you find the thread started by Andrej Mitrovic many years ago. He compared several implementations of (thread-safe) singletons. I it an extremely helpful stuff, IMHO.
|
Copyright © 1999-2021 by the D Language Foundation