www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D mixins

reply janderson <askme me.com> writes:
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
Jan 03 2007
next sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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)
Jan 04 2007
parent reply janderson <askme me.com> writes:
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
Jan 04 2007
next sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
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: And you use it like this: I believe mixins are just to save you from writing bolierplate code, I don't know if there are other uses... Ary
Jan 04 2007
parent BCS <nothing pathlink.com> writes:
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: And you use it like this: 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".
Jan 04 2007
prev sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
Jan 04 2007
parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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++?
Jan 04 2007
parent reply Waldemar <waldemar wa-ba.com> writes:
 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.
Jan 04 2007
parent reply mike <vertex gmx.at> writes:
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 go=
od =
 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 certai= n = scope: ' scope (failure) dosomething(); But, please, tell me I'm wrong, I would really need that :) -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 04 2007
next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
mike wrote:
 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/
Sadly, no, you cannot do that, because templates (perhaps mixins should be their own type? er, nevermind that) have their own evaluation scope, with the outer scope "friended" in. So, scope guards will execute on the templates's own scope rather than the host's. I've actually tried this, with my fingers crossed. All to no avail. (It would've been a darn nifty way to do selective tracebacks. But alas.) -- Chris Nicholson-Sauls
Jan 04 2007
next sibling parent Waldemar <waldemar wa-ba.com> writes:
== Quote from Chris Nicholson-Sauls (ibisbasenji gmail.com)'s article
 mike wrote:
 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/
Sadly, no, you cannot do that, because templates (perhaps mixins should be their
own type?
   er, nevermind that) have their own evaluation scope, with the outer scope
"friended" in.
   So, scope guards will execute on the templates's own scope rather than the
host's. I've
 actually tried this, with my fingers crossed.  All to no avail.  (It would've
been a darn
 nifty way to do selective tracebacks.  But alas.)
 -- Chris Nicholson-Sauls
I was not aware of that, but the post was intended for C++ users. Most, if not all, C/C++ ugly macros can be nicely written as mixins in D. Now, getting back to D, I suppose one could write the scope functionality "by hand" (the "C++ way") into a mixin! Inserting it would produce the desired effect. That's a theory at the moment, I do not have a code.
Jan 04 2007
prev sibling parent mike <vertex gmx.at> writes:
Am 05.01.2007, 04:28 Uhr, schrieb Chris Nicholson-Sauls  =

<ibisbasenji gmail.com>:

 Sadly, no, you cannot do that, because templates (perhaps mixins shoul=
d =
 be their own type?   er, nevermind that) have their own evaluation  =
 scope, with the outer scope "friended" in.   So, scope guards will  =
 execute on the templates's own scope rather than the host's.  I've  =
 actually tried this, with my fingers crossed.  All to no avail.  (It  =
 would've been a darn nifty way to do selective tracebacks.  But alas.)=
 -- Chris Nicholson-Sauls
I wanted to do tracing this way as well (see my other reply). I've = actually started to go through all my code and copy/paste the scopeguard= s = at the beginning of each function. But, mixin or not, it's just too much= = to insert those statement in 30k or so lines of code, I'll rather just t= ry = out if the modified phobos that was linked to a couple of times here in = = the ng works with 1.0 :) -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 05 2007
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
mike wrote:
 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();
It's just a wild guess, but... are you trying to get a backtrace of some sort ? If so, you might want to try the one from Shinichiro: http://shinh.skr.jp/d/ -- Tomasz Stachowiak
Jan 04 2007
parent reply mike <vertex gmx.at> writes:
Am 05.01.2007, 05:11 Uhr, schrieb Tom S <h3r3tic remove.mat.uni.torun.pl=
:
 It's just a wild guess, but... are you trying to get a backtrace of so=
me =
 sort ? If so, you might want to try the one from Shinichiro:  =
 http://shinh.skr.jp/d/


 --
 Tomasz Stachowiak
Exactly. I've seen that a couple of days ago, downloaded it, but I didn't dare to= = install it, because it says it's for version 0.176 or so and I wanted to= = wait until it's officially supporting 1.0 and do the mixin backtrace in = = the meantime. Does it work with 1.0? Any experience? -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 05 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
mike wrote:
 Am 05.01.2007, 05:11 Uhr, schrieb Tom S <h3r3tic remove.mat.uni.torun.pl>:
 It's just a wild guess, but... are you trying to get a backtrace of 
 some sort ? If so, you might want to try the one from Shinichiro: 
 http://shinh.skr.jp/d/
Exactly. I've seen that a couple of days ago, downloaded it, but I didn't dare to install it, because it says it's for version 0.176 or so and I wanted to wait until it's officially supporting 1.0 and do the mixin backtrace in the meantime. Does it work with 1.0? Any experience?
No experience, sorry. But the binary is pretty much guaranteed not to work; there have been some ABI changes since v0.176. You could try diffing the provided sources to the originals from DMD 0.176 and applying the patches to the current Phobos though.
Jan 05 2007
parent reply Sean Kelly <sean f4.ca> writes:
Frits van Bommel wrote:
 mike wrote:
 Am 05.01.2007, 05:11 Uhr, schrieb Tom S 
 <h3r3tic remove.mat.uni.torun.pl>:
 It's just a wild guess, but... are you trying to get a backtrace of 
 some sort ? If so, you might want to try the one from Shinichiro: 
 http://shinh.skr.jp/d/
Exactly. I've seen that a couple of days ago, downloaded it, but I didn't dare to install it, because it says it's for version 0.176 or so and I wanted to wait until it's officially supporting 1.0 and do the mixin backtrace in the meantime. Does it work with 1.0? Any experience?
No experience, sorry. But the binary is pretty much guaranteed not to work; there have been some ABI changes since v0.176. You could try diffing the provided sources to the originals from DMD 0.176 and applying the patches to the current Phobos though.
I think it's just some changes to object.d, deh.c, deh2.d, dmain2.d, from internal, and a new module in std: symtable.d. The merge should be pretty straightforward. Sean
Jan 05 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Sean Kelly wrote:
 Frits van Bommel wrote:
 You could try diffing the provided sources to the originals from DMD 
 0.176 and applying the patches to the current Phobos though.
I think it's just some changes to object.d, deh.c, deh2.d, dmain2.d, from internal, and a new module in std: symtable.d. The merge should be pretty straightforward.
That's why I suggested trying it :).
Jan 05 2007
prev sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
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
Jan 04 2007