std.mixins

BCS none at anon.com
Tue Aug 31 11:43:54 PDT 2010


Hello dsimcha,

> == Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s
> article
> 
>> On 8/30/10 20:04 PDT, dsimcha wrote:
>> 
>>> I've been toying for a long time with the idea of a std.mixins for
>>> Phobos that would contain meta-implementations of commonly needed
>>> boilerplate code for mixing into classes and and structs.  I've
>>> started to prototype it
>>> (http://dsource.org/projects/scrapple/browser/trunk/std_mixins/std_m
>>> ixins.d). So far I have a mixin for struct comparisons, which is
>>> useful if you need a total ordering for use with sorting or binary
>>> trees, but don't care exactly how that ordering is defined.  I've
>>> also got a mixin that converts a class to a Singleton, and uses
>>> thread-safe but efficient mechanisms to deal with the __gshared
>>> singleton case.
>>> 
>> Upon a second look I think the Singleton implementation is not up to
>> snuff and reflects a common misunderstanding of the pattern.
>> The point of the Singleton is that you can instantiate it with a
>> derived
>> class. Otherwise, it's the less interesting Monostate pattern. The
>> code
>> as is right now uses hardcoded calls to new, and it shouldn't.
>> Andrei
> Interersting.  The Wikipedia examples don't seem to allow this.  Can
> you elaborate?  How would the class know when to use which derived
> class?
> 

A proposed API:

template Singleton(T, string name, bool populate = true, D = T) {
bool SetOrDie(U, A...)(A a);
T SetOrGet(U, A...)(A a);
T Set(U, A...)(A a);
T Get();
}
void SingletonClearAll();

useage:

alias Singleton!(Foo, "bar") foo;

foo.Get().Whatever();
foo.SetOrGet!(FooSub)().Whatever(); // same as get
foo.Set!(FooSub)();  // over write

SingletonClearAll();

foo.SetOrGet!(FooSub)(); // same as set

foo.SetOrDie!(FooSub)(); // already set;

foo.



-- 
... <IXOYE><





More information about the Digitalmars-d mailing list