OpDispatcher

Mixin template generating an opDispatch redirecting calls to members whose names match the passed variable string but with a given token string in the front or at the end of the name.

Members

Functions

opDispatch
auto ref opDispatch(T value)

Mutator.

opDispatch
auto ref opDispatch()

Accessor.

Parameters

token

The token to look for as part of the variable name, either in the front of it or at the end of it. May be any non-zero number of characters.

inFront

Whether to look for the token in front of the variable name instead of at the end of it; defaults to Yes.inFront.

Examples

1 {
2     static struct Foo
3     {
4         int _i;
5         string _s;
6         bool _b;
7         string[] _add;
8         alias wordList = _add;
9 
10         mixin OpDispatcher!("_", Yes.inFront);
11     }
12 
13     Foo f;
14     f.i = 42;         // f.opDispatch!"i"(42);
15     f.s = "hello";    // f.opDispatch!"s"("hello");
16     f.b = true;       // f.opDispatch!"b"(true);
17     f.add("hello");   // f.opDispatch!"add"("hello");
18     f.add("world");   // f.opDispatch!"add"("world");
19 
20     assert(f.i == 42);
21     assert(f.s == "hello");
22     assert(f.b);
23     assert(f.wordList == [ "hello", "world" ]);
24 
25     // ref auto allows this
26     ++f.i;
27     assert(f.i == 43);
28 
29     /+
30         Returns `this` by reference, so we can chain calls.
31      +/
32     auto f2 = Foo()
33         .i(9001)
34         .s("world")
35         .b(false)
36         .add("hi")
37         .add("there");
38 
39     assert(f2.i == 9001);
40     assert(f2.s == "world");
41     assert(!f2.b);
42     assert(f2.wordList == [ "hi", "there" ]);
43 }
44 {
45     static struct  Foo
46     {
47         int i_private;
48         string s_private;
49         bool b_private = true;
50         string[] add_private;
51         alias wordList = add_private;
52 
53         mixin OpDispatcher!("_private", No.inFront);
54     }
55 
56     Foo f;
57     f.i = 9;
58     f.s = "foo";
59     f.b = false;
60     f.add("bar");
61     f.add("baz");
62 
63     assert(f.i == 9);
64     assert(f.s == "foo");
65     assert(!f.b);
66     assert(f.wordList == [ "bar", "baz" ]);
67 }