Is a template like copy-paste?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Fri Oct 27 06:18:10 PDT 2006


Ary Manzana wrote:
> I have a template for singleton:
> 
> ---
> template Singleton() {
> 
>    private static typeof(this) _instance;
>    private this() { }
> 
>    public static typeof(this) instance() {
>        if (!_instance) {
>            _instance = new typeof(this)();
>        }
>        return _instance;
>    }
> 
> }
> ---
> 
> I use it like this:
> 
> ---
> class Bla { // Line 15
> 
>    mixin Singleton!();
> 
> }
> 
> void main() {
>     Bla bla = Bla.instance;
> }
> ---
> 
> The compiler says:
> main.d(15): class main.Bla main.Bla.Singleton!().this is private
> 
> Then I copy-paste the template into the class definition, to see what 
> happens:
> 
> ---
> class Bla {
> 
>    private static typeof(this) _instance;
>    private this() { }
> 
>    public static typeof(this) instance() {
>        if (!_instance) {
>            _instance = new typeof(this)();
>        }
>        return _instance;
>    }
> 
> }
> 
> void main() {
>     Bla bla = Bla.instance;
> }
> ---
> 
> Compiles fine.
> 
> If I change "private this() { }" to "protected this() { }" in the 
> singleton template, both compile fine. However, this is just a 
> workaround, the first should also work.

First off, very nifty to use typeof() in a Singleton template!  I never even tried that. 
Secondly, consider moving the _instance static variable into the instance() function... 
maybe its just me, but I tend to find that cleaner.  :)  Different strokes, maybe.

Third, technically no templates aren't quite like copy-paste.  The issue is, mixins have 
their own scope.  Protected is the way to go; besides, it makes your Singleton class 
derivable.  Consider it a bonus feature.  Personally, I'd always just put the instance() 
function into the template, and expected the class designer to be intelligent enough to 
make the constructor protected/private, otherwise you're screwed if your Singleton needs 
to do any startup work.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-bugs mailing list