Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Template Functions

Part of TemplatesCategory

Description

Collections are just way to much fun to putter around with.

Here's an example that results in calls that look like

 TmixCollectionTypes!(Object,String).addAll(foo,bar);

when you want to copy from one contained type to another.

Example

interface Collection(T) { // a cheesy Collection
    T getItem();
    void addItem(T x);
}

template TmixCollectionTypes(S,T) {
    void addAll(Collection!(S) x, Collection!(T) y) {
        x.addItem(y.getItem()); // you get the idea...

  }
}

class LinkedList(T): Collection!(T) { // a cheesy list
    T getItem() { return item; }
    void addItem(T x) { item=x; }
    T item;
}

class String {}

int main(char[][] argv)
{
    Collection!(Object) foo = new LinkedList!(Object);
    Collection!(String) bar = new LinkedList!(String);
    TmixCollectionTypes!(Object,String).addAll(foo,bar);
    return 0;
}

Source

From D:24532 by Ben Hinkle.