Jump to page: 1 2
Thread overview
D mixins
Jan 04, 2007
janderson
Jan 04, 2007
Lutger
Jan 04, 2007
janderson
Jan 04, 2007
Ary Manzana
Jan 04, 2007
BCS
Jan 04, 2007
Lutger
Jan 04, 2007
Waldemar
Jan 05, 2007
mike
Jan 05, 2007
Waldemar
Jan 05, 2007
mike
Jan 05, 2007
Tom S
Jan 05, 2007
mike
Jan 05, 2007
Frits van Bommel
Jan 05, 2007
Sean Kelly
Jan 05, 2007
Frits van Bommel
Jan 04, 2007
Tom S
January 04, 2007
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
January 04, 2007
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)
January 04, 2007
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
January 04, 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
January 04, 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".
January 04, 2007
janderson wrote:
> 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

I think the Signals/Slots implementation in Phobos is a good example.

-- Chris Nicholson-Sauls
January 04, 2007
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

They're a convenient tool for inserting parametrized code into scopes - in some cases this can be partially accomplished in C++ through the (ab)use of multiple inheritance. For instance:

template <typename SomePolicy>
struct Foo : SomePolicy {
};

could be coded in D as:

struct Foo(SomePolicy) {
    mixin SomePolicy;
}

It doesn't seem like a big win in this case, but when the Policy must use the Foo struct for some reason, the C++ approach would be to specialize the policy on the struct's type, like
struct Foo : SomePolicy<Foo>

while in D, the mixin just knows about the struct's type because mixing it in means that it becomes a part of the struct's declaration, so for instance typeof(this) inside a mixin will work just like in the struct itself.

D further benefits from the fact that mixins can be easily named - it helps avoid naming conflicts.


--
Tomasz Stachowiak
January 04, 2007
Chris Nicholson-Sauls wrote:
> janderson wrote:
>> 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

Yes indeed, this is not a good example vis-a-vis C++.

Private inheritance in C++ is sometimes called mixin. So here, inheritance is used (but more restrictive) to emulate mixin's.

I find the seperation between (single) inheritance, interface and raw code injection better conceptually in D. In C++ it's all lumped together in inheritance, which makes it harder to distinguish intent.

There surely are good examples, I haven't used mixins myself in such a way that you are asking for.

Of course it is a unconvincing argument to make, but I would rather ask the question if there are any good examples where MI a la C++ is cleaner than interface and / or mixins because MI is so ugly it's on par with preproccesor macros and template template arguments.

> I think the Signals/Slots implementation in Phobos is a good example.
> 
> -- Chris Nicholson-Sauls

I think you can achieve this as easy with multiple inheritance in C++?
January 04, 2007
> >> 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?

Roughly speaking, anytime you see a multiline #define macro in C++ (especially with parameters), you would use a mixin in D.

That's only one sample use, but it should speak to the Cplusplusers.

January 05, 2007
Am 05.01.2007, 00:19 Uhr, schrieb Waldemar <waldemar@wa-ba.com>:

>> >> 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?
>
> Roughly speaking, anytime you see a multiline #define macro in C++ (especially
> with parameters), you would use a mixin in D.
>
> That's only one sample use, but it should speak to the Cplusplusers.
>

Erm ... I don't think you can insert this line via a mixin into a certain scope:

' scope (failure) dosomething();

But, please, tell me I'm wrong, I would really need that :)

-mike

-- 
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
« First   ‹ Prev
1 2