Re: Singleton Pattern

2016-07-10 Thread yawniek via Digitalmars-d-learn
On Thursday, 5 January 2012 at 22:15:32 UTC, asm wrote: how can i implementing the singleton pattern in D? https://p0nce.github.io/d-idioms/#Leveraging-TLS-for-a-fast-thread-safe-singleton

Re: Singleton Pattern

2016-07-09 Thread Suliman via Digitalmars-d-learn
On Saturday, 9 July 2016 at 20:47:32 UTC, Ali Çehreli wrote: On 07/09/2016 01:35 PM, Suliman wrote: > On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote: >> On 01/05/2012 02:15 PM, asm wrote: >>> how can i implementing the singleton pattern in D? >> >

Re: Singleton Pattern

2016-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 07/09/2016 01:35 PM, Suliman wrote: > On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote: >> On 01/05/2012 02:15 PM, asm wrote: >>> how can i implementing the singleton pattern in D? >> >> Is singleton still alive? ;) I'm glad I was alive in 2012

Re: Singleton Pattern

2016-07-09 Thread Suliman via Digitalmars-d-learn
On Thursday, 5 January 2012 at 22:53:12 UTC, Ali Çehreli wrote: On 01/05/2012 02:15 PM, asm wrote: how can i implementing the singleton pattern in D? Is singleton still alive? ;) An idea is to instantiate the object in the module's static this(). Ali Yeah, same question, what diffe

Re: Singleton Pattern with struct

2013-01-24 Thread Era Scarecrow
On Thursday, 24 January 2013 at 16:49:53 UTC, Maxim Fomin wrote: On Thursday, 24 January 2013 at 16:17:34 UTC, Era Scarecrow wrote: Hmmm. You could separate the data and remove the pointer... then use alias this. [code] struct Singleton { static SingletonData single; alias single this;

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
On Thursday, 24 January 2013 at 17:35:58 UTC, Ali Çehreli wrote: On 01/24/2013 09:26 AM, ParticlePeter wrote: > Thanks, I re-read the purpose of ref type function() in the D > programming language, and the sole purpose is that such a function call > can be directly a parameter to another functio

Re: Singleton Pattern with struct

2013-01-24 Thread Artur Skawina
On 01/24/13 18:35, ParticlePeter wrote: > On Thursday, 24 January 2013 at 17:21:38 UTC, Artur Skawina wrote: >> On 01/24/13 17:52, ParticlePeter wrote: >>> Why is the s inside the struct and another_s not identical ? >>> Afaik that is the purpose of the ref keyword ? >> >> There currently are no re

Re: Singleton Pattern with struct

2013-01-24 Thread Ali Çehreli
On 01/24/2013 09:26 AM, ParticlePeter wrote: > Thanks, I re-read the purpose of ref type function() in the D > programming language, and the sole purpose is that such a function call > can be directly a parameter to another function expecting a ref ? As Maxim Fomin noted, I didn't word it correc

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
On Thursday, 24 January 2013 at 17:21:38 UTC, Artur Skawina wrote: On 01/24/13 17:52, ParticlePeter wrote: Yes, but this can be broken by: import core.stdc.stdio : printf; struct Singleton { private : this( int a = 0 ) {} ; static Singleton * s ; public : @disable th

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
On Thursday, 24 January 2013 at 17:00:44 UTC, Ali Çehreli wrote: On 01/24/2013 08:52 AM, ParticlePeter wrote: > This method here ( my first approach ) does return a reference to an > object on the heap, right ? Yes, but the caller does not get a reference. > static ref Singleton instance() { >

Re: Singleton Pattern with struct

2013-01-24 Thread Artur Skawina
On 01/24/13 18:14, Artur Skawina wrote: > struct Singleton { > private: > this( int a = 0 ) {} ; > static Singleton* s ; > > public: > @disable this(); > @disable this(this); > static instance() @property { > static struct Ref(T) { T* obj; ref g() @property { return *o

Re: Singleton Pattern with struct

2013-01-24 Thread Artur Skawina
On 01/24/13 17:52, ParticlePeter wrote: >> Yes, but this can be broken by: >> >> import core.stdc.stdio : printf; >> >> struct Singleton { >> >> private : >> this( int a = 0 ) {} ; >> static Singleton * s ; >> >> public : >> @disable this() ; >> static Singleton* in

Re: Singleton Pattern with struct

2013-01-24 Thread Maxim Fomin
On Thursday, 24 January 2013 at 17:00:44 UTC, Ali Çehreli wrote: On 01/24/2013 08:52 AM, ParticlePeter wrote: > This method here ( my first approach ) does return a reference to an > object on the heap, right ? Yes, but the caller does not get a reference. Actually the caller gets the referen

Re: Singleton Pattern with struct

2013-01-24 Thread Ali Çehreli
On 01/24/2013 08:52 AM, ParticlePeter wrote: > This method here ( my first approach ) does return a reference to an > object on the heap, right ? Yes, but the caller does not get a reference. > static ref Singleton instance() { > if ( s is null ) > s = new Singleton( 0 ) ; > return * s ; > } >

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
Yes, but this can be broken by: import core.stdc.stdio : printf; struct Singleton { private : this( int a = 0 ) {} ; static Singleton * s ; public : @disable this() ; static Singleton* instance() { if ( s is null ) s = n

Re: Singleton Pattern with struct

2013-01-24 Thread Maxim Fomin
On Thursday, 24 January 2013 at 16:17:34 UTC, Era Scarecrow wrote: On Thursday, 24 January 2013 at 16:07:36 UTC, Maxim Fomin wrote: Yes, but this can be broken by: void main() { Singleton s = * Singleton.instance; printf( "%d\n", s.val ) ; // Singleton.instance.val = 2 ;

Re: Singleton Pattern with struct

2013-01-24 Thread Era Scarecrow
On Thursday, 24 January 2013 at 16:07:36 UTC, Maxim Fomin wrote: Yes, but this can be broken by: void main() { Singleton s = * Singleton.instance; printf( "%d\n", s.val ) ; // Singleton.instance.val = 2 ; printf( "%d\n", s.val ) ; //0 } Here s is explicitly def

Re: Singleton Pattern with struct

2013-01-24 Thread Maxim Fomin
On Thursday, 24 January 2013 at 15:50:34 UTC, ParticlePeter wrote: Got it, thanks, I changed the instance method to: [code] static Singleton * instance() { if ( s is null ) s = new Singleton( 0 ) ; return s ; } [\code] and

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
Got it, thanks, I changed the instance method to: [code] static Singleton * instance() { if ( s is null ) s = new Singleton( 0 ) ; return s ; } [\code] and everything works as expected. Cheers, PP !

Re: Singleton Pattern with struct

2013-01-24 Thread Maxim Fomin
On Thursday, 24 January 2013 at 15:05:15 UTC, Jacob Carlborg wrote: On 2013-01-24 15:43, Maxim Fomin wrote: Even if Singleton.instance returns by ref, s object is still stack-allocated struct, which is not affected by further modification of private pointer. The struct is allocated using "n

Re: Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
Even if Singleton.instance returns by ref, s object is still stack-allocated struct, which is not affected by further modification of private pointer. But where and when is ( a second ? ) Singleton created or duplicated ? The only ctor is in the static instance ( it is called only once accor

Re: Singleton Pattern with struct

2013-01-24 Thread Jacob Carlborg
On 2013-01-24 15:43, Maxim Fomin wrote: Even if Singleton.instance returns by ref, s object is still stack-allocated struct, which is not affected by further modification of private pointer. The struct is allocated using "new". -- /Jacob Carlborg

Re: Singleton Pattern with struct

2013-01-24 Thread Maxim Fomin
On Thursday, 24 January 2013 at 14:11:10 UTC, ParticlePeter wrote: Hi, I am trying to figure out the singleton pattern with a struct instead of a class: [code] struct Singleton { private : this( int a = 0 ) {} ; static Singleton * s ; public : @disable this

Singleton Pattern with struct

2013-01-24 Thread ParticlePeter
Hi, I am trying to figure out the singleton pattern with a struct instead of a class: [code] struct Singleton { private : this( int a = 0 ) {} ; static Singleton * s ; public : @disable this() ; static ref Singleton instance() { if ( s is

Re: Singleton Pattern

2012-01-11 Thread Jonathan M Davis
On Thursday, January 12, 2012 00:55:14 Thomas Mader wrote: > On Friday, January 6, 2012, Jonathan M Davis wrote: > > The same way that you would in C++ or Java, except that unlike you have > > static > > The most simple and elegant way in java is to use an enum. Is there nothing > comparable in

Re: Singleton Pattern

2012-01-11 Thread Thomas Mader
On Friday, January 6, 2012, Jonathan M Davis wrote: > The same way that you would in C++ or Java, except that unlike you have static The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?

Re: Singleton Pattern

2012-01-06 Thread asm
thank you, you answer is very helpful.

Re: Singleton Pattern

2012-01-05 Thread Jonathan M Davis
On Thursday, January 05, 2012 22:15:32 asm wrote: > how can i implementing the singleton pattern in D? The same way that you would in C++ or Java, except that unlike you have static constructors that you can use to initialize them, and unlike both, if it's not shared, then it's a

Re: Singleton Pattern

2012-01-05 Thread Ali Çehreli
On 01/05/2012 02:15 PM, asm wrote: how can i implementing the singleton pattern in D? Is singleton still alive? ;) An idea is to instantiate the object in the module's static this(). Ali

Re: Singleton Pattern

2012-01-05 Thread asm
both if you don't mind

Re: Singleton Pattern

2012-01-05 Thread Andrew Wiley
On Thu, Jan 5, 2012 at 4:15 PM, asm wrote: > how can i implementing the singleton pattern in D? Multithreaded or not?

Singleton Pattern

2012-01-05 Thread asm
how can i implementing the singleton pattern in D?