digitalmars.D - How to write a singleton template?
- Li Jie (61/61) Mar 14 2006 I try to write a singleton template, but has not succeeded:
-
Jarrett Billingsley
(10/11)
Mar 14 2006
"Li Jie"
wrote in message - Li Jie (10/19) Mar 14 2006 Thannnks.
- Kyle Furlong (39/120) Mar 14 2006 You might want to consider a mixin:
- Li Jie (7/45) Mar 15 2006 Thanks.
- Kyle Furlong (4/62) Mar 15 2006 Its in http://www.digitalmars.com/d/template.html, about 80% down the pa...
- Bruno Medeiros (20/44) Mar 15 2006 "because D can only mixin static things to classes"
- Kyle Furlong (2/50) Mar 15 2006 The documentation leads one to believe that this is impossible. Look und...
- Bruno Medeiros (8/60) Mar 19 2006 The limitations section is about instantiating templates that are
- Kyle Furlong (3/62) Mar 19 2006 It is misleading because it asserts "Templates cannot be used to add non...
- Bruno Medeiros (7/72) Mar 20 2006 The term, "Inner templates" should be clear enough then, as in "Inner
- Frank Benoit (5/6) Mar 15 2006 string not null terminated, change to one of
I try to write a singleton template, but has not succeeded: A: template Singleton(T) { private T _instance; static this() { _instance = new T(); } public T instance () { return _instance; } } B: template Singleton(T) { class Singleton { private static T _instance; static this() { _instance = new T(); } public static T instance () { return _instance; } } } C: template Singleton(T) { class Singleton { private static T _instance = null; public static T instance () { if (_instance == null) _instance = new T(); return _instance; } } } // use it: class AAA { public void hello () { printf("hello\n"); } } int main(char[][] args) { alias Singleton!(AAA) aaa; aaa.instance().hello(); // <== Segment fault !!!!! return 0; } I want to know, how to write a singleton template? Thanks. - Li Jie
Mar 14 2006
"Li Jie" <cpunion gmail.com> wrote in message news:dv7s7e$1hee$1 digitaldaemon.com... Versions A and B work fine, but version C doesn't work because of this line:if (_instance == null)Change it to if(_instance is null) This is because the == operator calls opEquals on the first object, so _instance == null is the same as _instance.opEquals(null) - which is obviously going to cause a segfault if _instance is null. Use the "is" (and the "!is") operator to determine what a class reference is pointing to (like null).
Mar 14 2006
In article <dv80jr$1m1g$1 digitaldaemon.com>, Jarrett Billingsley says...Versions A and B work fine, but version C doesn't work because of this line:Thannnks. It works find. But, Versions A and B work can't work on my computer. Segment fault at the same line, and I don't know why. My system: P4 3.0G, 1G RAM, Gentoo 2006.0 gcc 3.4.4, dmd 0.149 Thanks again. - Li Jieif (_instance == null)Change it to if(_instance is null) This is because the == operator calls opEquals on the first object, so _instance == null is the same as _instance.opEquals(null) - which is obviously going to cause a segfault if _instance is null. Use the "is" (and the "!is") operator to determine what a class reference is pointing to (like null).
Mar 14 2006
Li Jie wrote:I try to write a singleton template, but has not succeeded: A: template Singleton(T) { private T _instance; static this() { _instance = new T(); } public T instance () { return _instance; } } B: template Singleton(T) { class Singleton { private static T _instance; static this() { _instance = new T(); } public static T instance () { return _instance; } } } C: template Singleton(T) { class Singleton { private static T _instance = null; public static T instance () { if (_instance == null) _instance = new T(); return _instance; } } } // use it: class AAA { public void hello () { printf("hello\n"); } } int main(char[][] args) { alias Singleton!(AAA) aaa; aaa.instance().hello(); // <== Segment fault !!!!! return 0; } I want to know, how to write a singleton template? Thanks. - Li JieYou might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); } Or with inheritance: // Note here that there is some syntactic sugar for templated classes class Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } protected static T _instance; } class OnlyOne : Singleton!(OnlyOne) { private this() {} }
Mar 14 2006
In article <dv8anc$2109$1 digitaldaemon.com>, Kyle Furlong says...You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); } Or with inheritance: // Note here that there is some syntactic sugar for templated classes class Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } protected static T _instance; } class OnlyOne : Singleton!(OnlyOne) { private this() {} }Thanks. In the version 2, must 'Singleton' template class and 'OnlyOne' class be in the same file? I've never seen the usage of 'class XXX(T)', I read 'class.html' and 'template.html', but not found. Which document mention it? - Li Jie
Mar 15 2006
Li Jie wrote:In article <dv8anc$2109$1 digitaldaemon.com>, Kyle Furlong says...No, D templates can be in separate modules, just import the module you put the template in.You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); } Or with inheritance: // Note here that there is some syntactic sugar for templated classes class Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } protected static T _instance; } class OnlyOne : Singleton!(OnlyOne) { private this() {} }Thanks. In the version 2, must 'Singleton' template class and 'OnlyOne' class be in the same file?I've never seen the usage of 'class XXX(T)', I read 'class.html' and 'template.html', but not found. Which document mention it? - Li JieIts in http://www.digitalmars.com/d/template.html, about 80% down the page, with the heading Class Templates. What is undocumented is the fact that struct templates are also legal. (a very nice feature IMHO)
Mar 15 2006
Kyle Furlong wrote:You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); }"because D can only mixin static things to classes" -> That is incorrect, you can include instance methods and constructors just fine. See for yourself in a example such as this: template Baz() { public void func() { writefln(x); } public this() { writefln("Construct!"); } } class Foo { int x = 2; mixin Baz!(); } ... (new Foo).func(); The only problem is with private protection attributes and constructors, as reported in bug: news://news.digitalmars.com:119/bug-49-3 http.d.puremagic.com/bugzilla/ -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Mar 15 2006
Bruno Medeiros wrote:Kyle Furlong wrote:The documentation leads one to believe that this is impossible. Look under the limitations header. This needs to be clarified.You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); }"because D can only mixin static things to classes" -> That is incorrect, you can include instance methods and constructors just fine. See for yourself in a example such as this: template Baz() { public void func() { writefln(x); } public this() { writefln("Construct!"); } } class Foo { int x = 2; mixin Baz!(); } ... (new Foo).func(); The only problem is with private protection attributes and constructors, as reported in bug: news://news.digitalmars.com:119/bug-49-3 http.d.puremagic.com/bugzilla/
Mar 15 2006
Kyle Furlong wrote:Bruno Medeiros wrote:The limitations section is about instantiating templates that are defined inside a class, it is not about mixing in in a template inside a class, which is a different thing. The limitation exists only in the first case. This behaviour make sense. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DKyle Furlong wrote:The documentation leads one to believe that this is impossible. Look under the limitations header. This needs to be clarified.You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); }"because D can only mixin static things to classes" -> That is incorrect, you can include instance methods and constructors just fine. See for yourself in a example such as this: template Baz() { public void func() { writefln(x); } public this() { writefln("Construct!"); } } class Foo { int x = 2; mixin Baz!(); } ... (new Foo).func(); The only problem is with private protection attributes and constructors, as reported in bug: news://news.digitalmars.com:119/bug-49-3 http.d.puremagic.com/bugzilla/
Mar 19 2006
Bruno Medeiros wrote:Kyle Furlong wrote:It is misleading because it asserts "Templates cannot be used to add non-static members or functions to classes." Even with the example, its still a blanket, general statement. It should specify that the example is the only case which has the limitation.Bruno Medeiros wrote:The limitations section is about instantiating templates that are defined inside a class, it is not about mixing in in a template inside a class, which is a different thing. The limitation exists only in the first case. This behaviour make sense.Kyle Furlong wrote:The documentation leads one to believe that this is impossible. Look under the limitations header. This needs to be clarified.You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); }"because D can only mixin static things to classes" -> That is incorrect, you can include instance methods and constructors just fine. See for yourself in a example such as this: template Baz() { public void func() { writefln(x); } public this() { writefln("Construct!"); } } class Foo { int x = 2; mixin Baz!(); } ... (new Foo).func(); The only problem is with private protection attributes and constructors, as reported in bug: news://news.digitalmars.com:119/bug-49-3 http.d.puremagic.com/bugzilla/
Mar 19 2006
Kyle Furlong wrote:Bruno Medeiros wrote:The term, "Inner templates" should be clear enough then, as in "Inner Templates cannot be used to add non-static members or functions to classes." ? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DKyle Furlong wrote:It is misleading because it asserts "Templates cannot be used to add non-static members or functions to classes." Even with the example, its still a blanket, general statement. It should specify that the example is the only case which has the limitation.Bruno Medeiros wrote:The limitations section is about instantiating templates that are defined inside a class, it is not about mixing in in a template inside a class, which is a different thing. The limitation exists only in the first case. This behaviour make sense.Kyle Furlong wrote:The documentation leads one to believe that this is impossible. Look under the limitations header. This needs to be clarified.You might want to consider a mixin: template Singleton(T) { static this() { _instance = new T(); } public static T instance () { return _instance; } private static T _instance; } class OnlyOne { // Can't include this in the mixin, // because D can only mixin static things to classes private this() {} mixin Singleton!(OnlyOne); }"because D can only mixin static things to classes" -> That is incorrect, you can include instance methods and constructors just fine. See for yourself in a example such as this: template Baz() { public void func() { writefln(x); } public this() { writefln("Construct!"); } } class Foo { int x = 2; mixin Baz!(); } ... (new Foo).func(); The only problem is with private protection attributes and constructors, as reported in bug: news://news.digitalmars.com:119/bug-49-3 http.d.puremagic.com/bugzilla/
Mar 20 2006
printf("hello\n");string not null terminated, change to one of printf("hello\n\x00"); writef("hello\n"); writefln("hello"); Frank
Mar 15 2006