-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2_opDispatch.d
57 lines (45 loc) · 1.02 KB
/
_2_opDispatch.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
unittest
{
import std.variant : Variant;
struct C {
void callA(int i, int j) {
assert(i == 1);
assert(j == 2);
}
void callB(string s) {
assert(s == "ABC");
}
}
struct CallLogger(C) {
C content;
void opDispatch(string name, T...)(T vals) {
mixin("content." ~ name)(vals);
}
}
struct var {
private Variant[string] values;
@property
Variant opDispatch(string name)() const {
return values[name];
}
@property
void opDispatch(string name, T)(T val) {
values[name] = val;
}
}
import std.stdio : writeln;
var test;
test.foo = "test";
test.bar = 50;
test.foobar = 3.1415;
assert(test.foo == "test");
assert(test.bar == 50);
assert(test.foobar == 3.1415);
/*
* Do not modify parameters
*/
CallLogger!C l;
l.callA(1, 2);
l.callB("ABC");
writeln("Test #2 passed");
}