D mixins

Ary Manzana ary at esperanto.org.ar
Thu Jan 4 09:52:15 PST 2007


janderson escribió:
> Lutger wrote:
>> janderson wrote:
>>> I'm trying to explain to a friend on msn what mixins are.  Can you
>>> provide some good examples, that would be difficult otherwise?
>>>
>>> -Joel
>>
>> What about implementing multiple inheritance in a language that does 
>> not have multiple inheritance? (interface + mixin as default 
>> implementation)
> 
> Thanks for your reply.
> 
> Well coming from C++ this would not make much sense.  Where would you 
> use them in C++?  Where would you use them in D?  Are there any good D 
> examples that show the power of mixins in a simple way that would be 
> more ugly another way?
> 
> Cheers.
> -Joel

The singleton pattern is a nice and real world example of some use for 
mixins:

# template Singleton() {
#
#    protected static typeof(this) _instance;
#    protected this() { }
#
#    public static typeof(this) instance() {
#        if (!_instance) {
#            _instance = new typeof(this)();
#        }
#        return _instance;
#    }
#
# }

And you use it like this:

# class MySingletonClass {
#
#     mixin Singleton!();
#
# }

I believe mixins are just to save you from writing bolierplate code, I 
don't know if there are other uses...

Ary



More information about the Digitalmars-d mailing list