Re: Singleton in Action?
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.
Re: How can I walk the list in a RegexMatch backwards?
On Sunday, 3 February 2019 at 18:07:13 UTC, Chris Bare wrote: auto matches = matchAll(str, searchRegex); foreach (m; matches) // this walks the list forward I tried: foreach_reverse (m; matches) foreach (m; reverse (matches)) foreach (m; retro (matches)) and they all failed to compile. I also tried to index matches (matches[i]) but that does not work either. matchAll is a forward range, whereas retro requires a bi-directional range. So using the array function should work: foreach (m; matches.array.retro) Jordan
Re: Singleton in Action?
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
How can I walk the list in a RegexMatch backwards?
auto matches = matchAll(str, searchRegex); foreach (m; matches) // this walks the list forward I tried: foreach_reverse (m; matches) foreach (m; reverse (matches)) foreach (m; retro (matches)) and they all failed to compile. I also tried to index matches (matches[i]) but that does not work either.
Re: static arrays at runtime with templates ?
On Sunday, 3 February 2019 at 16:36:30 UTC, Paul Backus wrote: ... This is actually already in the standard library: https://dlang.org/phobos/std_array.html#staticArray thank you either I already read that and forgot or finally I'm starting to get the feel of the language :)
Re: static arrays at runtime with templates ?
On Sunday, 3 February 2019 at 16:33:48 UTC, Emil wrote: I tried this on a whim and it appears to work: auto static_array(T, size_t data_size)() { T[data_size] data; return data; } This is actually already in the standard library: https://dlang.org/phobos/std_array.html#staticArray
static arrays at runtime with templates ?
I tried this on a whim and it appears to work: auto static_array(T, size_t data_size)() { T[data_size] data; return data; } void main() { import std.stdio: writeln; import std.traits: isStaticArray; auto data = static_array!(int, 5)(3); writeln(data); static assert(__traits(isStaticArray, data)); auto data_longer = static_array!(int, 6)(2); writeln(data_longer); static assert(__traits(isStaticArray, data_longer)); } Is this for real, static arrays at runtime without manually allocating memory ? Is this legitimate or should I expect problems ?
Re: Singleton in Action?
On Sun, 2019-02-03 at 14:42 +, 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 Roadm: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk signature.asc Description: This is a digitally signed message part
Re: Singleton in Action?
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.)
Re: Singleton in Action?
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.
Re: Singleton in Action?
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
Re: Singleton in Action?
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/
Re: Singleton in Action?
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.
Re: Singleton in Action?
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
Re: Singleton in Action?
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.