import std.stdio; import MultiDisFunction; class Shape { } class Circle : Shape { } class Rect : Shape { } bool checkCollisionCC(Circle circle1, Circle circle2) { writefln("in checkCollisionCC"); return false; } bool checkCollisionCR(Circle circle, Rect rect) { writefln("in checkCollisionCR"); return true; } bool checkCollisionRC(Rect rect, Circle circle) { writefln("in checkCollisionRC"); return checkCollisionCR(circle, rect); } bool checkCollisionRR(Rect rect1, Rect rect2) { writefln("in checkCollisionRR"); return false; } // test functions: char someOtherFunc1(Rect rect1, Rect rect2) { return 1; } bool someOtherFunc2(Object obj1, int obj2) { return 1; } bool someOtherFunc3(Object obj1, Object obj2) { return 1; } bool someOtherFunc4(Object obj1, Object obj2, Object obj3) { return 1; } /***** MDF Usage *****/ static this() { // Define the MDF dispatcher MultiDispatchFunction!(bool function(Shape, Shape)) checkCollisionMDF; // IFTI doesn't work here: //checkCollisionMDF.AddDispatch(&checkCollisionCC); checkCollisionMDF.AddDispatch!(typeof(&checkCollisionCC))(&checkCollisionCC); checkCollisionMDF.AddDispatch!(typeof(&checkCollisionCR))(&checkCollisionCR); checkCollisionMDF.AddDispatch!(typeof(&checkCollisionRC))(&checkCollisionRC); checkCollisionMDF.AddDispatch!(typeof(&checkCollisionRR))(&checkCollisionRR); checkCollisionMDF.call(new Circle, new Circle); checkCollisionMDF.call(new Rect, new Rect); checkCollisionMDF.call(new Circle, new Rect); checkCollisionMDF.call(new Rect, new Circle); checkCollisionMDF.call(new Rect, typeid(void)); // should fail }