root/trunk/src/tutorials/examples/collection/collection.d

Revision 6, 1.4 kB (checked in by jcc7, 3 years ago)

Made more progress in developing convertToWiki and added some files.

Line 
1 /+
2 Title: Re: template functions
3 Author: "Ben Hinkle" <bhinkle4@juno.com>
4 Date: Mon, 23 Feb 2004 14:19:47 -0500
5
6 "Sean Kelly" <sean@ffwd.cx> wrote in message
7 news:c1derl$2bif$1@digitaldaemon.com...
8 > Ben Hinkle wrote:
9 > >
10 > > So my first guess is that templates might not be the right way to do
11 what
12 > > you want to do. That doesn't mean there isn't a problem with templates
13 > > but I'm not sure this is a great example.
14 >
15 > I think it's more an example of how the syntax can get complicated
16 > rather than being an example of something you'd probably want to do very
17 > often.
18 >
19 >
20 > Sean
21 >
22
23 Take Two. Collections are just waay to much fun to putter around with :-)
24 Here's an example that results in calls that look like
25   TmixCollectionTypes!(Object,String).addAll(foo,bar);
26 when you want to copy from one contained type to another.
27 More details:
28 +/
29
30
31 interface Collection(T) { // a cheesy Collection
32     T getItem();
33     void addItem(T x);
34 }
35
36 template TmixCollectionTypes(S,T) {
37     void addAll(Collection!(S) x, Collection!(T) y) {
38         x.addItem(y.getItem()); // you get the idea...
39     }
40 }
41
42 class LinkedList(T): Collection!(T) { // a cheesy list
43     T getItem() { return item; }
44     void addItem(T x) { item=x; }
45     T item;
46 }
47
48 class String {}
49
50 int main(char[][] argv)
51 {
52     Collection!(Object) foo = new LinkedList!(Object);
53     Collection!(String) bar = new LinkedList!(String);
54     TmixCollectionTypes!(Object,String).addAll(foo,bar);
55     return 0;
56 }
Note: See TracBrowser for help on using the browser.