Mutator.
Accessor.
1 { 2 struct Foo 3 { 4 int _i; 5 string _s; 6 bool _b; 7 string[] _add; 8 alias wordList = _add; 9 10 mixin UnderscoreOpDispatcher; 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("hello") 37 .add("world"); 38 39 assert(f2.i == 9001); 40 assert(f2.s == "world"); 41 assert(!f2.b); 42 assert(f2.wordList == [ "hello", "world" ]); 43 } 44 { 45 struct Bar 46 { 47 string[string] _aa; 48 49 mixin UnderscoreOpDispatcher; 50 } 51 52 Bar bar; 53 bar.aa = [ "hello" : "world" ]; 54 bar.aa = [ "foo" : "bar" ]; 55 assert(bar.aa == [ "hello" : "world", "foo" : "bar"]); 56 }
Mixin template generating an opDispatch redirecting calls to members whose names match the passed variable string but with an underscore prepended.