digitalmars.D.learn - Singleton in Action?
- Ron Tarrant (50/50) Feb 02 2019 Hi guys,
- Russel Winder (28/46) Feb 02 2019 and gals.
- Eugene Wissner (19/69) Feb 02 2019 Imho it looks fine.
- Neia Neutuladh (2/10) Feb 02 2019 And consider putting the class in its own source file.
- Ron Tarrant (9/10) Feb 03 2019 Yes, by all means.
- Alex (3/13) Feb 03 2019 Isn't deriving a singleton even eviler as having one? ;)
- Ron Tarrant (7/8) Feb 03 2019 Perhaps this was meant as rhetoric, but I think you may be right.
- Russel Winder (23/35) Feb 03 2019 There is a lot of good stuff (both positive and negative) on Singleton h...
- Ron Tarrant (9/20) Feb 04 2019 Yup. I've gone back and forth on usage, too. The cases I found
- Kagamin (8/11) Feb 04 2019 It's usually replaced with inversion of control: the service
- Ron Tarrant (4/4) Feb 02 2019 Thanks for the replies, fellow programmers. (generic, unisex, PC,
- Andre Pany (5/9) Feb 02 2019 I found here an example:
- Ron Tarrant (2/6) Feb 03 2019 Thanks, Andre. Exactly what I was hoping for.
- Ron Tarrant (4/5) Feb 03 2019 Do you know if this is for a current version of D? The compiler
- Jonathan M Davis (7/12) Feb 03 2019 I don't recall std.thread ever existing, and std.c.time hasn't been arou...
- Ron Tarrant (3/9) Feb 03 2019 Thanks for clearing that up, Jonathan.
- angel (4/8) Feb 02 2019 I think that's what you really need:
- Norm (8/13) Feb 02 2019 If you haven't already been to the d-idioms site it is well worth
- Norm (10/60) Feb 02 2019 Sorry, I should read the post fully before replying, my bad. You
- Tony (14/19) Feb 02 2019 This seems to be a case where D's definition of "private" can
- Jacob Carlborg (20/30) Feb 03 2019 You don't need to make it so complicated. Here's a simpler example:
- Ron Tarrant (14/20) Feb 04 2019 My understanding is that in D, this line effectively says: the
- Alex (23/45) Feb 04 2019 No, it can.
- Jacob Carlborg (12/23) Feb 04 2019 Yes, well it's called once. Technically since the protections in D work
- Ron Tarrant (3/7) Feb 06 2019 Okay, that explains a few things that I still hadn't caught onto.
- Dejan Lekic (4/12) Feb 03 2019 I strongly suggest you find the thread started by Andrej Mitrovic
- Ron Tarrant (2/5) Feb 04 2019 Thanks. I'll see if I can find it.
- bauss (10/60) Feb 04 2019 Your code is not going to work in the way you think.
- kdevel (14/31) Feb 05 2019 [...]
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. Here's the singleton code I've been playing with: class DSingleton { private: // Cache instantiation flag in thread-local bool // Thread local static bool instantiated_; // Thread global __gshared DSingleton instance_; this() { } // this() public: static DSingleton get() { if(!instantiated_) { synchronized(DSingleton.classinfo) { if(!instance_) { instance_ = new DSingleton(); writeln("creating"); } instantiated_ = true; } } else { writeln("not created"); } return(instance_); } // DSingleton() } // class DSingleton So, my big question is, do I instantiate like this: DSingleton singleton = new DSingleton; Or like this: DSingleton singleton = singleton.get(); And subsequent calls would be...? The same? Using get() only?
Feb 02 2019
On Sat, 2019-02-02 at 16:56 +0000, Ron Tarrant via Digitalmars-d-learn wrot= e:Hi guys,and gals.I ran into another snag this morning while trying to implement a=20 singleton. I found all kinds of examples of singleton=20 definitions, but nothing about how to put them into practice.General, these days, as far as I know, Singleton is considered one of the ultimate anti-patterns of software development. There are many positive reasons for this very negative view of Singleton especially if there is any hint of concurrency or parallelism involved. Also Singletons tend to deny sensible testing.Can someone show me a code example for how one would actually use=20 a singleton pattern in D? When I did the same thing in PHP, it=20 took me forever to wrap my brain around it, so I'm hoping to get=20 there a little faster this time.I'd suggest just not going here. Let's not have Singleton in D code. Alternatives include "Parameterize From Above" which I believe Kevlin Henne= y pushes strongly, and indeed he may be the source of the "pattern". (I belie= ve it is a design not a pattern using the proper definition of pattern.) If the resource really is "singleton" then creating an actor or agent is no= w the usual solution in languages such as D, Rust, Go, even C++.=20[=E2=80=A6]So, my big question is, do I instantiate like this: =20 DSingleton singleton =3D new DSingleton;This seems wrong.Or like this: =20 DSingleton singleton =3D singleton.get();getting seems like the one true way, if you really have to do this. Can I indent that Singleton not be part of the canon of D programming.And subsequent calls would be...? The same? Using get() only? =20 =20--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Feb 02 2019
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. Here's the singleton code I've been playing with: class DSingleton { private: // Cache instantiation flag in thread-local bool // Thread local static bool instantiated_; // Thread global __gshared DSingleton instance_; this() { } // this() public: static DSingleton get() { if(!instantiated_) { synchronized(DSingleton.classinfo) { if(!instance_) { instance_ = new DSingleton(); writeln("creating"); } instantiated_ = true; } } else { writeln("not created"); } return(instance_); } // DSingleton() } // class DSingleton So, my big question is, do I instantiate like this: DSingleton singleton = new DSingleton; Or like this: DSingleton singleton = singleton.get(); And subsequent calls would be...? The same? Using get() only?Imho it looks fine. For creation get() should be always used, since it is the most convenient way to ensure that there is really only one instance of the singleton. Just make this() private, so only you can create new instances: private this() { } And you probably don't need instantiated_, you can always check whether instance_ is null or not. So: if (instance_ is null) { ... } else { ... }
Feb 02 2019
On Sat, 02 Feb 2019 17:34:11 +0000, Eugene Wissner wrote:For creation get() should be always used, since it is the most convenient way to ensure that there is really only one instance of the singleton. Just make this() private, so only you can create new instances: private this() { }And consider putting the class in its own source file.
Feb 02 2019
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.
Feb 03 2019
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:Isn't deriving a singleton even eviler as having one? ;) https://codeblog.jonskeet.uk/2006/01/19/singleton-inheritance/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.
Feb 03 2019
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.)
Feb 03 2019
On Sun, 2019-02-03 at 14:42 +0000, Ron Tarrant via Digitalmars-d-learn wrot= e:On Sunday, 3 February 2019 at 10:28:51 UTC, Alex wrote: =20There 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 wor= th 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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.ukIsn't deriving a singleton even eviler as having one? ;)=20 Perhaps this was meant as rhetoric, but I think you may be right. =20 This morning I was Googling "singleton replacement" and someone=20 on another forum said Factory would do the job. Anyone have=20 thoughts on that? =20 (Personally, I don't see it, but I'm willing to update my=20 position based on new evidence.)
Feb 03 2019
On Sunday, 3 February 2019 at 15:33:15 UTC, Russel Winder wrote: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-singletonsThanks, Russell. I'll take a look.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.Yup. I've gone back and forth on usage, too. The cases I found for a Singleton ten years ago may have better solutions today. Like a command dispatcher, a configuration changes tracker, a keyboard pre-processor, an undo/redo stack, and the application itself.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.I'd be curious to hear what you have/will replace a singleton with.
Feb 04 2019
On Sunday, 3 February 2019 at 14:42:13 UTC, Ron Tarrant wrote:This morning I was Googling "singleton replacement" and someone on another forum said Factory would do the job. Anyone have thoughts on that?It's usually replaced with inversion of control: the service instance is passed as an argument to object constructors, a related pattern is service provider when you need to access many services. Though it needs a more involved code organization compared to singleton. As for testing, singletons can be tested if you provide a setter method that allows to set a custom singleton instance.
Feb 04 2019
Thanks for the replies, fellow programmers. (generic, unisex, PC, and all-encompassing) If I could trouble someone for a complete working example so I have something to study, that would be excellent.
Feb 02 2019
On Saturday, 2 February 2019 at 19:23:58 UTC, Ron Tarrant wrote:Thanks for the replies, fellow programmers. (generic, unisex, PC, and all-encompassing) If I could trouble someone for a complete working example so I have something to study, that would be excellent.I found here an example: https://rosettacode.org/wiki/Singleton#D Kind regards Andre
Feb 02 2019
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 AndreThanks, Andre. Exactly what I was hoping for.
Feb 03 2019
On Saturday, 2 February 2019 at 19:40:25 UTC, Andre Pany wrote:https://rosettacode.org/wiki/Singleton#DDo 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
Feb 03 2019
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: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 Davishttps://rosettacode.org/wiki/Singleton#DDo 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
Feb 03 2019
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 DavisThanks for clearing that up, Jonathan.
Feb 03 2019
On Saturday, 2 February 2019 at 19:23:58 UTC, Ron Tarrant wrote:Thanks for the replies, fellow programmers. (generic, unisex, PC, and all-encompassing) If I could trouble someone for a complete working example so I have something to study, that would be excellent.I think that's what you really need: [1] https://davesdprogramming.wordpress.com/ [2] https://www.youtube.com/watch?v=ZHmIAdlNtiM
Feb 02 2019
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. [...]If you haven't already been to the d-idioms site it is well worth a look: https://p0nce.github.io/d-idioms/ It has a singleton example that you may find useful. https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-singleton bye, norm
Feb 02 2019
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. Here's the singleton code I've been playing with: class DSingleton { private: // Cache instantiation flag in thread-local bool // Thread local static bool instantiated_; // Thread global __gshared DSingleton instance_; this() { } // this() public: static DSingleton get() { if(!instantiated_) { synchronized(DSingleton.classinfo) { if(!instance_) { instance_ = new DSingleton(); writeln("creating"); } instantiated_ = true; } } else { writeln("not created"); } return(instance_); } // DSingleton() } // class DSingleton So, my big question is, do I instantiate like this: DSingleton singleton = new DSingleton; Or like this: DSingleton singleton = singleton.get(); And subsequent calls would be...? The same? Using get() only?Sorry, I should read the post fully before replying, my bad. You access the singleton via the get() function whenever you need it. It is static so there's no need to create a copy of the instance in a "singleton" variable. DSingleton singleton = new DSingleton; is bad. It bypasses all the checks in the "get()" method to ensure it is a singleton and outside the module where you defined DSingleton it won't compile. bye, norm
Feb 02 2019
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:So, my big question is, do I instantiate like this: DSingleton singleton = new DSingleton; Or like this: DSingleton singleton = singleton.get(); And subsequent calls would be...? The same? Using get() only?This seems to be a case where D's definition of "private" can cause confusion versus examples from other languages with private as "access only inside the class". In other languages DSingleton singleton = new DSingleton; would never compile (the desired behavior), since the constructor is private. But D allows it in some cases due to "private to class OR module". If DSingleton is in the same module, as for example in a small test program with DSingleton and main() in the same file, then it compiles. So I think your code is fine, it's just that it can be circumvented - the private constructor can be called - if DSingledton and the instantiating code are in the same module.
Feb 02 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
Feb 03 2019
On Sunday, 3 February 2019 at 18:53:10 UTC, Jacob Carlborg wrote:You don't need to make it so complicated. Here's a simpler example:Excellent. Thank you. Simple is best.private __gshared auto instance_ = new DSingleton;My understanding is that in D, this line effectively says: the singleton is created at compile time and can't be changed, ever. Is that a fair assessment?private this() // private to make sure no one else can create an instanceI've seen comments similar to this in several examples. When you say "no one else" you're personifying callers? And so this means: No caller outside the object? Which really amounts to: Since no one INside the object WILL call this() and no one OUTside CAN call this(), it won't ever get called.writeln(DSingleton.instance);No '()' needed for the call to DSingleton.instance? If it's called again from somewhere else, say from within an object function several levels of scope away, it's called the same way?
Feb 04 2019
On Monday, 4 February 2019 at 10:36:49 UTC, Ron Tarrant wrote:On Sunday, 3 February 2019 at 18:53:10 UTC, Jacob Carlborg wrote:No, it can. ´´´ class DSingleton { private __gshared auto instance_ = new DSingleton; size_t state; private this(){} // private to make sure no one else can create an instance static DSingleton instance() { return instance_; } } void main() { assert(DSingleton.instance.state == 0); DSingleton.instance.state = 5; assert(DSingleton.instance.state == 5); } ´´´You don't need to make it so complicated. Here's a simpler example:Excellent. Thank you. Simple is best.private __gshared auto instance_ = new DSingleton;My understanding is that in D, this line effectively says: the singleton is created at compile time and can't be changed, ever. Is that a fair assessment?To some extent...private this() // private to make sure no one else can create an instanceI've seen comments similar to this in several examples. When you say "no one else" you're personifying callers?And so this means: No caller outside the object? Which really amounts to: Since no one INside the object WILL call this() and no one OUTside CAN call this(), it won't ever get called.I think, what is meant is: The class has to be placed alone in a module so that no one outside the class (and the module) has any direct access to object creation routine.Yes. https://dlang.org/spec/function.html#optional-parenthesiswriteln(DSingleton.instance);No '()' needed for the call to DSingleton.instance? If it's called again from somewhere else, say from within an object function several levels of scope away, it's called the same way?
Feb 04 2019
On 2019-02-04 11:36, Ron Tarrant wrote:I've seen comments similar to this in several examples. When you say "no one else" you're personifying callers?Yes.And so this means: No caller outside the object? Which really amounts to: Since no one INside the object WILL call this() and no one OUTside CAN call this(), it won't ever get called.Yes, well it's called once. Technically since the protections in D work on module level a caller inside the same file can access the constructor. But one is usually in control of the whole file.No need. It's the way D to implement properties.writeln(DSingleton.instance);No '()' needed for the call to DSingleton.instance?If it's called again from somewhere else, say from within an object function several levels of scope away, it's called the same way?You can call it with or without parentheses. It applies to all functions that don't take any arguments or functions taking a single argument and are called using UFCS [1]. [1] https://dlang.org/spec/function.html#pseudo-member -- /Jacob Carlborg
Feb 04 2019
On Monday, 4 February 2019 at 19:23:26 UTC, Jacob Carlborg wrote:You can call it with or without parentheses. It applies to all functions that don't take any arguments or functions taking a single argument and are called using UFCS [1]. [1] https://dlang.org/spec/function.html#pseudo-memberOkay, that explains a few things that I still hadn't caught onto. Thanks.
Feb 06 2019
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.
Feb 03 2019
On Sunday, 3 February 2019 at 22:25:18 UTC, Dejan Lekic wrote: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.Thanks. I'll see if I can find it.
Feb 04 2019
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. Here's the singleton code I've been playing with: class DSingleton { private: // Cache instantiation flag in thread-local bool // Thread local static bool instantiated_; // Thread global __gshared DSingleton instance_; this() { } // this() public: static DSingleton get() { if(!instantiated_) { synchronized(DSingleton.classinfo) { if(!instance_) { instance_ = new DSingleton(); writeln("creating"); } instantiated_ = true; } } else { writeln("not created"); } return(instance_); } // DSingleton() } // class DSingleton So, my big question is, do I instantiate like this: DSingleton singleton = new DSingleton; Or like this: DSingleton singleton = singleton.get(); And subsequent calls would be...? The same? Using get() only?Your code is not going to work in the way you think. Ex. you state "instantiated_" is thread-local but that's the flag you use to check whether it has been instantiated. That will not work. Instead it should actually be shared, especially because you use it in a synchronized statement. Also: DSingleton singleton = new DSingleton; Defeats the purpose of singleton.
Feb 04 2019
On Monday, 4 February 2019 at 10:17:53 UTC, bauss wrote:On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:[...][...]Here's the singleton code I've been playing with:[...]static bool instantiated_; // Thread global __gshared DSingleton instance_;[...]if(!instantiated_)[...]if(!instance_)Ex. you state "instantiated_" is thread-local but that's the flag you use to check whether it has been instantiated.User angel has pointed to https://davesdprogramming.wordpress.com/ and the talk https://www.youtube.com/watch?v=ZHmIAdlNtiM which entails the reasoning about that pattern.That will not work.Will it not work in therory (in which?) or in practice (in which)?Instead it should actually be shared, especially because you use it in a synchronized statement.The purpose of the pattern is to avoid synchronisationin all but the first call to get().Also: DSingleton singleton = new DSingleton; Defeats the purpose of singleton.Sure, but it is not lazy.
Feb 05 2019