forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvd.d
151 lines (125 loc) · 4.22 KB
/
mvd.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/++
mvd stands for Multiple Virtual Dispatch. It lets you
write functions that take any number of arguments of
objects and match based on the dynamic type of each
of them.
---
void foo(Object a, Object b) {} // 1
void foo(MyClass b, Object b) {} // 2
void foo(DerivedClass a, MyClass b) {} // 3
Object a = new MyClass();
Object b = new Object();
mvd!foo(a, b); // will call overload #2
---
The return values must be compatible; [mvd] will return
the least specialized static type of the return values
(most likely the shared base class type of all return types,
or `void` if there isn't one).
All non-class/interface types should be compatible among overloads.
Otherwise you are liable to get compile errors. (Or it might work,
that's up to the compiler's discretion.)
+/
module arsd.mvd;
import std.traits;
/// This exists just to make the documentation of [mvd] nicer looking.
alias CommonReturnOfOverloads(alias fn) = CommonType!(staticMap!(ReturnType, __traits(getOverloads, __traits(parent, fn), __traits(identifier, fn))));
/// See details on the [arsd.mvd] page.
CommonReturnOfOverloads!fn mvd(alias fn, T...)(T args) {
return mvdObj!fn(null, args);
}
CommonReturnOfOverloads!fn mvdObj(alias fn, This, T...)(This this_, T args) {
typeof(return) delegate() bestMatch;
int bestScore;
string argsStr() {
string s;
foreach(arg; args) {
if(s.length)
s ~= ", ";
static if (is(typeof(arg) == class)) {
if (arg is null) {
s ~= "null " ~ typeof(arg).stringof;
} else {
s ~= typeid(arg).name;
}
} else {
s ~= typeof(arg).stringof;
}
}
return s;
}
ov: foreach(overload; __traits(getOverloads, __traits(parent, fn), __traits(identifier, fn))) {
Parameters!overload pargs;
int score = 0;
foreach(idx, parg; pargs) {
alias t = typeof(parg);
static if(is(t == interface) || is(t == class)) {
pargs[idx] = cast(typeof(parg)) args[idx];
if(args[idx] !is null && pargs[idx] is null)
continue ov; // failed cast, forget it
else
score += BaseClassesTuple!t.length + 1;
} else
pargs[idx] = args[idx];
}
if(score == bestScore)
throw new Exception("ambiguous overload selection with args (" ~ argsStr ~ ")");
if(score > bestScore) {
bestMatch = () {
static if(is(typeof(return) == void))
__traits(child, this_, overload)(pargs);
else
return __traits(child, this_, overload)(pargs);
};
bestScore = score;
}
}
if(bestMatch is null)
throw new Exception("no match existed with args (" ~ argsStr ~ ")");
return bestMatch();
}
///
unittest {
class MyClass {}
class DerivedClass : MyClass {}
class OtherClass {}
static struct Wrapper {
static: // this is just a namespace cuz D doesn't allow overloading inside unittest
int foo(Object a, Object b) { return 1; }
int foo(MyClass a, Object b) { return 2; }
int foo(DerivedClass a, MyClass b) { return 3; }
int bar(MyClass a) { return 4; }
}
with(Wrapper) {
assert(mvd!foo(new Object, new Object) == 1);
assert(mvd!foo(new MyClass, new DerivedClass) == 2);
assert(mvd!foo(new DerivedClass, new DerivedClass) == 3);
assert(mvd!foo(new OtherClass, new OtherClass) == 1);
assert(mvd!foo(new OtherClass, new MyClass) == 1);
assert(mvd!foo(new DerivedClass, new DerivedClass) == 3);
assert(mvd!foo(new OtherClass, new MyClass) == 1);
//mvd!bar(new OtherClass);
}
}
///
unittest {
class MyClass {}
class DerivedClass : MyClass {}
class OtherClass {}
class Wrapper {
int x;
int foo(Object a, Object b) { return x + 1; }
int foo(MyClass a, Object b) { return x + 2; }
int foo(DerivedClass a, MyClass b) { return x + 3; }
int bar(MyClass a) { return x + 4; }
}
Wrapper wrapper = new Wrapper;
wrapper.x = 20;
assert(wrapper.mvdObj!(wrapper.foo)(new Object, new Object) == 21);
assert(wrapper.mvdObj!(wrapper.foo)(new MyClass, new DerivedClass) == 22);
assert(wrapper.mvdObj!(wrapper.foo)(new DerivedClass, new DerivedClass) == 23);
assert(wrapper.mvdObj!(wrapper.foo)(new OtherClass, new OtherClass) == 21);
assert(wrapper.mvdObj!(wrapper.foo)(new OtherClass, new MyClass) == 21);
assert(wrapper.mvdObj!(wrapper.foo)(new DerivedClass, new DerivedClass) == 23);
assert(wrapper.mvdObj!(wrapper.foo)(new OtherClass, new MyClass) == 21);
//mvd!bar(new OtherClass);
}