View mode: basic / threaded / horizontal-split · Log in · Help
October 02, 2005
maybe it's a bug
In my program I have several classes which I would like to use as singletons

And I made this little template:
------------------------
template Singleton(T)
{
public static this(){ m_instance = new T(); }
public static T get(){ return m_instance; }
private static T m_instance;
private this(){}
}
// and this is how it's used:
class Foo
{
mixin Singleton!(Foo);
}
------------------------
The following compile error occured:
test.d(23): class test.Foo test.Foo.Singleton!(...) Singleton_C4test3Foo.this is
private

While the next code works perfectly:
------------------------
template Singleton(T)
{
public static this(){ m_instance = new T(); }
public static T get(){ return m_instance; }
private static T m_instance;
private this(){}
}
class Foo
{
mixin Singleton!(Foo);
private this(){} // <- only this added
}
------------------------
So it looks like both mixin and class define the same private constructor twice.
This is strange. If asked, I would rather think that the first code sample would
compile and the second would not.
October 03, 2005
Re: maybe it's a bug
"Kostya Lisiy" <Kostya_member@pathlink.com> wrote in message 
news:dhphp1$luc$1@digitaldaemon.com...

I used a Singleton template class almost exactly the same as yours, except 
when I wanted to make a class a singleton, I'd use

class SomeSingleton : Singleton!(SomeSingleton)
{
   ...
}

And I wouldn't get the error.
October 03, 2005
Re: maybe it's a bug
In article <dhq1lr$100m$1@digitaldaemon.com>, Jarrett Billingsley says...
>
>"Kostya Lisiy" <Kostya_member@pathlink.com> wrote in message 
>news:dhphp1$luc$1@digitaldaemon.com...
>
>I used a Singleton template class almost exactly the same as yours, except 
>when I wanted to make a class a singleton, I'd use
>
>class SomeSingleton : Singleton!(SomeSingleton)
>{
>    ...
>}
>
>And I wouldn't get the error. 
>
>

But then you can not derive your class from another class - the inheritance is
used to make it a singleton.
October 03, 2005
Re: maybe it's a bug
Maybe it is not a bug but is the implementation of the mixin not what you like.
See, your template's static this() creates an instance called T. This T
translates to Foo. From the same template, you've got, this(), being declared
privately.

I understand that you want to happen that the private is for methods calling the
class from outside the scope of the class. However, private is already private
within the template, and therefore, can't be called within the class through new
Foo() in the static this().

I don't know whether this is a bug, bug it looks weird indeed...

Regards,
Sjoerd

In article <dhphp1$luc$1@digitaldaemon.com>, Kostya Lisiy says...
>
>In my program I have several classes which I would like to use as singletons
>
>And I made this little template:
>------------------------
>template Singleton(T)
>{
>public static this(){ m_instance = new T(); }
>public static T get(){ return m_instance; }
>private static T m_instance;
>private this(){}
>}
>// and this is how it's used:
>class Foo
>{
>mixin Singleton!(Foo);
>}
>------------------------
>The following compile error occured:
>test.d(23): class test.Foo test.Foo.Singleton!(...) Singleton_C4test3Foo.this is
>private
>
>While the next code works perfectly:
>------------------------
>template Singleton(T)
>{
>public static this(){ m_instance = new T(); }
>public static T get(){ return m_instance; }
>private static T m_instance;
>private this(){}
>}
>class Foo
>{
>mixin Singleton!(Foo);
>private this(){} // <- only this added
>}
>------------------------
>So it looks like both mixin and class define the same private constructor twice.
>This is strange. If asked, I would rather think that the first code sample would
>compile and the second would not.
>
>
October 04, 2005
Re: maybe it's a bug
"Kostya Lisiy" <Kostya_member@pathlink.com> wrote in message 
news:dhrf7l$25dq$1@digitaldaemon.com...
> But then you can not derive your class from another class - the 
> inheritance is
> used to make it a singleton.

Ooh, that's true.  I never needed that, so I never noticed that shortcoming.

Well, in that case, I'd chalk up the mixin bug as yet another example of D's 
really weird or just broken protection attributes.  That, or it's working 
the way it's supposed to, but it's just not very useful.
Top | Discussion index | About this forum | D home