www.digitalmars.com         C & C++   DMDScript  

D - D templates are officially awesome

reply Andy Friesen <andy ikagames.com> writes:
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
Jan 25 2004
next sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
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();
 }
Jan 25 2004
parent reply roel.mathys yucom.be writes:
Gat damn dude!

That is good!
This is getting better and better. I can't find this syntax documented. bye, roel
Jan 25 2004
parent Matthias Becker <Matthias_member pathlink.com> writes:
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
Jan 26 2004
prev sibling parent reply "Achilleas Margaritis" <axilmar b-online.gr> writes:
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();
 }
Jan 26 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
Jan 26 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
"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??
Jan 27 2004
prev sibling parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
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.
Jan 26 2004
parent reply Andy Friesen <andy ikagames.com> writes:
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
Jan 26 2004
next sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
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
Jan 27 2004
prev sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
You've got an extra semi in there, dude!

"Andy Friesen" <andy ikagames.com> wrote in message
news:bv4dvr$2mt9$1 digitaldaemon.com...
 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
Jan 27 2004