D mixins
BCS
nothing at pathlink.com
Thu Jan 4 12:17:03 PST 2007
Ary Manzana wrote:
> 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
With the use of tuples and static foreach, they can also be used to
insert generated code into a scope. this is handy if the generated code
needs to access user defined code
I just posed about an example of this (and a lot more template work)
over in digitalmars.D.announce "D Template based paser generator in 137
loc".
More information about the Digitalmars-d
mailing list