Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 26, 2004 D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Attachments: | Test code attached. The output is: C A C B C D A D C B C D C D B We don't need no stinking multiple inheritance! -- andy |
January 26, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | Gat damn dude! That is good! I didn't realize you could define class templates like that... when did that change? I thought use of the template keyword was still required! Probably when the ! system went live. I should pay attention... Good stuff Walter! Sean "Andy Friesen" <andy@ikagames.com> wrote in message news:bv1ljc$14em$1@digitaldaemon.com... > Test code attached. The output is: > > C A > C B > C D A > D C B > C D C D B > > We don't need no stinking multiple inheritance! > > -- andy > ---------------------------------------------------------------------------- ---- > > interface I { void foo();} > class A : I { void foo() { printf("A\n"); } } > class B : I { void foo() { printf("B\n"); } } > > class C(T) : T { > void foo() { > printf("C "); > super.foo(); > } > } > > class D(T) : T { > void foo() > { > printf("D "); > super.foo(); > } > } > > alias C!(A) CA; > alias C!(B) CB; > alias C!(D!(A)) CDA; > alias D!(C!(B)) DCB; > > alias C!(D!(C!(D!(B)))) CDCDB; > > void main() > { > I ca = new CA(); > I cb = new CB(); > ca.foo(); > cb.foo(); > > I cda = new CDA(); > I dcb = new DCB(); > > cda.foo(); > dcb.foo(); > > I cdcdb = new CDCDB(); > cdcdb.foo(); > } |
January 26, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | >Gat damn dude!
>
>That is good!
This is getting better and better.
I can't find this syntax documented.
bye,
roel
|
January 26, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to roel.mathys | >This is getting better and better.
>I can't find this syntax documented.
I looked for it, too, but it seems like it's still missing. But it was introduced three versions back :O
|
January 26, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team. "Andy Friesen" <andy@ikagames.com> wrote in message news:bv1ljc$14em$1@digitaldaemon.com... > Test code attached. The output is: > > C A > C B > C D A > D C B > C D C D B > > We don't need no stinking multiple inheritance! > > -- andy > ---------------------------------------------------------------------------- ---- > > interface I { void foo();} > class A : I { void foo() { printf("A\n"); } } > class B : I { void foo() { printf("B\n"); } } > > class C(T) : T { > void foo() { > printf("C "); > super.foo(); > } > } > > class D(T) : T { > void foo() > { > printf("D "); > super.foo(); > } > } > > alias C!(A) CA; > alias C!(B) CB; > alias C!(D!(A)) CDA; > alias D!(C!(B)) DCB; > > alias C!(D!(C!(D!(B)))) CDCDB; > > void main() > { > I ca = new CA(); > I cb = new CB(); > ca.foo(); > cb.foo(); > > I cda = new CDA(); > I dcb = new DCB(); > > cda.foo(); > dcb.foo(); > > I cdcdb = new CDCDB(); > cdcdb.foo(); > } > |
January 26, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Achilleas Margaritis | Achilleas Margaritis wrote: > From what I understand, you make a class inherit indirectly from itself, > using templates ? for example class C inherits from class D which inherits > from class C which inherits from D which inherits from B (in the case of > CDCDB) ? why do you consider this an important feature ? All will do is > confuse the other programmers of your team. The example I posted is silly, I know. Here's another example that's only a little less so: alias HttpStream!( // pull from http ZlibStream!( // decompress as we go TextReader!( // read line by line (or whatever) ExtraErrorChecking!( // throw IOError if anything so much as twitches the wrong way Object // have to end the expression somehow :) ))))) Something_That_Reads_Compressed_Text_From_a_Web_Server; It's weird, but it achieves the same effect as mixins or policies without much fuss. (casting to the superclasses is effectively useless, as each template instance is its own type; to alleviate this, you can make mixin classes inherit an interface as well as its template argument) By having each class override methods and chain them to the superclass, you could also forseeably do some neat things along the lines of the GoF decorator pattern. Anyway, I just thought it was a cool idiom. Maybe someone else will think up a better place to use it. :) -- andy |
January 27, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Achilleas Margaritis | You should not assume people are so unintelligent, incapable of learning. If they are, why the $%*&@ did you hire them in the first place? Sean "Achilleas Margaritis" <axilmar@b-online.gr> wrote in message news:bv3uj0$1slt$1@digitaldaemon.com... > From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team. |
January 27, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Sean L. Palmer wrote: > You should not assume people are so unintelligent, incapable of learning. > If they are, why the $%*&@ did you hire them in the first place? > To be fair, A, B, C, and D are pretty cryptic class names. I'd be confused too. :) "Real Programmers use C++ because they can do for (int i=a->b++;++c<=3+(d|3;);d*=++a-c%d)" -- andy |
January 27, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | I'll bite: that won't compile because (d|3;) is a syntax error. ;) Sean "Andy Friesen" <andy@ikagames.com> wrote in message news:bv4dvr$2mt9$1@digitaldaemon.com... > To be fair, A, B, C, and D are pretty cryptic class names. I'd be confused too. :) > > "Real Programmers use C++ because they can do > for (int i=a->b++;++c<=3+(d|3;);d*=++a-c%d)" > > -- andy |
January 28, 2004 Re: D templates are officially awesome | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | "Andy Friesen" <andy@ikagames.com> wrote in message news:bv47nq$2cnm$1@digitaldaemon.com... > Achilleas Margaritis wrote: > > From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits > > from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team. > > The example I posted is silly, I know. Here's another example that's only a little less so: > > alias > HttpStream!( // pull from http > ZlibStream!( // decompress as we go > TextReader!( // read line by line (or whatever) > ExtraErrorChecking!( // throw IOError if anything so much as > twitches the wrong way > Object // have to end the expression somehow :) > ))))) > Something_That_Reads_Compressed_Text_From_a_Web_Server; > > It's weird, but it achieves the same effect as mixins or policies without much fuss. (casting to the superclasses is effectively useless, as each template instance is its own type; to alleviate this, you can make mixin classes inherit an interface as well as its template argument) > > By having each class override methods and chain them to the superclass, you could also forseeably do some neat things along the lines of the GoF decorator pattern. > > Anyway, I just thought it was a cool idiom. Maybe someone else will think up a better place to use it. :) This is known as a bolt-in, and features large in several template libs, including ATL. Speaking modestly: this is chapter 22 of "Imperfect C++". :) But take heart Dear frienDs, these issue will also feature large in the D book. Reviewer volunteers?? |
Copyright © 1999-2021 by the D Language Foundation