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

Template Functions

Part of TemplatesCategory


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

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


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


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);
    return 0;


From D:24532 by Ben Hinkle.