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

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

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 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

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

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: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 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

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 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 =

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 ; } so

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

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* 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 *obj; }

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() { if

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

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

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 reference

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 function

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;