class allocators should be more encapsulated

Luís Marques luismarques at gmail.com
Fri Dec 29 07:11:43 PST 2006


Thomas Kuehne wrote:
> # class Some{
> #    int dummy;
> # 
> #    this(){
> #       static Some existing;
> #       if(existing is null){
> #          existing = this;
> #       }else{
> #          this = existing;
> #       }
> #    }
> # }
> # 
> # import std.stdio;
> # 
> # int main(){
> #    Some a = new Some();
> #    Some b = new Some();
> #    a.dummy = 13;
> #    writefln("b.dummy: %s", b.dummy);
> # 
> #    return 0;
> # }
> #
> 
> If you use this pattern alot, the GC will have to do some more cleaning.

I did not know that was possible (from my searches on google perhaps 
several other people didn't too?).

About the GC, might this make it better?

    this()
    {
       static Some existing;
       if(existing is null){
          existing = this;
       }else{
	 delete this;
          this = existing;
       }
    }

The only minor issue remaining is perhaps that some CPU cycles are 
wasted creating the unnecessary object?

Thanks for this information :)

--
Luís Marques



More information about the Digitalmars-d mailing list