tardy v0.0.1 - Runtime polymorphism without inheritance

Atila Neves atila.neves at gmail.com
Sat Jun 13 15:11:49 UTC 2020


https://code.dlang.org/packages/tardy
https://github.com/atilaneves/tardy

Tardy lets users have their cake and eat it too by not making 
them have to use classes for runtime polymorphism. No inheritance 
anywhere to be found, which means structs, ints, and arrays can 
be used with dynamic dispatch (classes as well). It uses the GC 
by default but users can specify their own allocator type for 
flexibility, including a built-in "small buffer optimisation" 
allocator. Sample usage code:

import tardy;

interface ITransformer {
     int transform(int) @safe pure const;
}
alias Transformer = Polymorphic!ITransformer;

int xform(Transformer t) {
     return t.transform(3);
}

struct Adder {
     int i;
     int transform(int j) @safe pure const { return i + j; }
}

struct Plus1 {
     int transform(int i) @safe pure const { return i + 1; }
}

unittest {
     assert(xform(Transformer(Adder(2))) == 5);
     assert(xform(Transformer(Adder(3))) == 6);

     assert(xform(Transformer(Plus1())) == 4);
}



More information about the Digitalmars-d-announce mailing list