digitalmars.D - D mixins
- janderson (3/3) Jan 03 2007 I'm trying to explain to a friend on msn what mixins are. Can you
- Lutger (3/7) Jan 04 2007 What about implementing multiple inheritance in a language that does not...
- janderson (8/16) Jan 04 2007 Thanks for your reply.
- Ary Manzana (25/45) Jan 04 2007 The singleton pattern is a nice and real world example of some use for
- BCS (7/58) Jan 04 2007 With the use of tuples and static foreach, they can also be used to
- Chris Nicholson-Sauls (3/23) Jan 04 2007 I think the Signals/Slots implementation in Phobos is a good example.
- Lutger (14/38) Jan 04 2007 Yes indeed, this is not a good example vis-a-vis C++.
- Waldemar (3/7) Jan 04 2007 Roughly speaking, anytime you see a multiline #define macro in C++ (espe...
- mike (13/22) Jan 04 2007 od =
- Chris Nicholson-Sauls (7/33) Jan 04 2007 Sadly, no, you cannot do that, because templates (perhaps mixins should ...
- Tom S (6/25) Jan 04 2007 It's just a wild guess, but... are you trying to get a backtrace of some...
- mike (14/20) Jan 05 2007 Exactly.
- Frits van Bommel (6/16) Jan 05 2007 No experience, sorry.
- Sean Kelly (5/23) Jan 05 2007 I think it's just some changes to object.d, deh.c, deh2.d, dmain2.d,
- Frits van Bommel (2/9) Jan 05 2007 That's why I suggested trying it :).
- Tom S (23/27) Jan 04 2007 They're a convenient tool for inserting parametrized code into scopes -
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
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? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)
Jan 04 2007
Lutger wrote:janderson wrote: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. -JoelI'm trying to explain to a friend on msn what mixins are. Can you provide some good examples, that would be difficult otherwise? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)
Jan 04 2007
janderson escribió:Lutger wrote: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... Aryjanderson wrote: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. -JoelI'm trying to explain to a friend on msn what mixins are. Can you provide some good examples, that would be difficult otherwise? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)
Jan 04 2007
Ary Manzana wrote:janderson escribió: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".Lutger wrote: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... Aryjanderson wrote: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. -JoelI'm trying to explain to a friend on msn what mixins are. Can you provide some good examples, that would be difficult otherwise? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)
Jan 04 2007
janderson wrote:Lutger wrote:I think the Signals/Slots implementation in Phobos is a good example. -- Chris Nicholson-Saulsjanderson wrote: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. -JoelI'm trying to explain to a friend on msn what mixins are. Can you provide some good examples, that would be difficult otherwise? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)
Jan 04 2007
Chris Nicholson-Sauls wrote:janderson wrote: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.Lutger wrote:janderson wrote: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. -JoelI'm trying to explain to a friend on msn what mixins are. Can you provide some good examples, that would be difficult otherwise? -JoelWhat about implementing multiple inheritance in a language that does not have multiple inheritance? (interface + mixin as default implementation)I think the Signals/Slots implementation in Phobos is a good example. -- Chris Nicholson-SaulsI think you can achieve this as easy with multiple inheritance in C++?
Jan 04 2007
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.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?
Jan 04 2007
Am 05.01.2007, 00:19 Uhr, schrieb Waldemar <waldemar wa-ba.com>:youWell coming from C++ this would not make much sense. Where would =od =use them in C++? Where would you use them in D? Are there any go=beDexamples that show the power of mixins in a simple way that would =Roughly speaking, anytime you see a multiline #define macro in =more ugly another way?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
mike wrote:Am 05.01.2007, 00:19 Uhr, schrieb Waldemar <waldemar wa-ba.com>: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-SaulsErm ... 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/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.good DWell 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 anyexamples that show the power of mixins in a simple way that would be more ugly another way?
Jan 04 2007
== Quote from Chris Nicholson-Sauls (ibisbasenji gmail.com)'s articlemike wrote:own type?Am 05.01.2007, 00:19 Uhr, schrieb Waldemar <waldemar wa-ba.com>:Sadly, no, you cannot do that, because templates (perhaps mixins should be theirErm ... 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/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.good DWell 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 anyexamples that show the power of mixins in a simple way that would be more ugly another way?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 thehost's. I'veactually tried this, with my fingers crossed. All to no avail. (It would'vebeen a darnnifty way to do selective tracebacks. But alas.) -- Chris Nicholson-SaulsI 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
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-SaulsI 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
mike wrote:Am 05.01.2007, 00:19 Uhr, schrieb Waldemar <waldemar wa-ba.com>: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 StachowiakErm ... I don't think you can insert this line via a mixin into a certain scope: ' scope (failure) dosomething();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.good DWell 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 anyexamples that show the power of mixins in a simple way that would be more ugly another way?
Jan 04 2007
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 StachowiakExactly. 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
mike wrote:Am 05.01.2007, 05:11 Uhr, schrieb Tom S <h3r3tic remove.mat.uni.torun.pl>: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.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?
Jan 05 2007
Frits van Bommel wrote:mike wrote: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. SeanAm 05.01.2007, 05:11 Uhr, schrieb Tom S <h3r3tic remove.mat.uni.torun.pl>: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.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?
Jan 05 2007
Sean Kelly wrote:Frits van Bommel wrote:That's why I suggested trying it :).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.
Jan 05 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? -JoelThey'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