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
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
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
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
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
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 !
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
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
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 ;
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 =
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
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
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() {
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; }
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
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
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
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
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
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;
20 matches
Mail list logo