digitalmars.D.learn - opDispatch with string mixin does not work as I would expect.
- German Diago (22/22) Feb 09 2018 Hello everyone,
- Boris-Barboris (15/17) Feb 09 2018 struct Outer
- Nicholas Wilson (5/32) Feb 09 2018 That looks like it should work. Perhaps you need to change
- German Diago (4/11) Feb 10 2018 Do not ask me why but now seems to work with my initial solution.
Hello everyone, I am trying to forward some member functions from a struct as a Catch-all function, so I did something like this: struct A { struct HeaderData { align(1): char [21] id; ubyte field1; ubyte field2; } Nullable!(HeaderData) headerData; auto opDispatch(string name)() { readHeader(); static foreach (member; __traits(allMembers, HeaderData)) { static if (name == member) { return mixin("headerData." ~ name); } } } The mixin line does not work. I want to generate the access to the field. How could I achieve that?
Feb 09 2018
On Saturday, 10 February 2018 at 06:32:43 UTC, German Diago wrote:The mixin line does not work. I want to generate the access to the field. How could I achieve that?struct Outer { struct Inner { int a; float b; } Inner i; auto opDispatch(string name)() { return __traits(getMember, i, name); } }
Feb 09 2018
On Saturday, 10 February 2018 at 06:32:43 UTC, German Diago wrote:Hello everyone, I am trying to forward some member functions from a struct as a Catch-all function, so I did something like this: struct A { struct HeaderData { align(1): char [21] id; ubyte field1; ubyte field2; } Nullable!(HeaderData) headerData; auto opDispatch(string name)() { readHeader(); static foreach (member; __traits(allMembers, HeaderData)) { static if (name == member) { return mixin("headerData." ~ name); } } } The mixin line does not work. I want to generate the access to the field. How could I achieve that?That looks like it should work. Perhaps you need to change `static if (name == member)` to `static if (name == member.stringof)` Alternatively you could do something likeauto opDispatch(string name)() if(hasMember!(HeaderData,name){ readHeader(); return mixin("headerData." ~ name); }
Feb 09 2018
On Saturday, 10 February 2018 at 07:47:58 UTC, Nicholas Wilson wrote:On Saturday, 10 February 2018 at 06:32:43 UTC, German DiagoAlternatively you could do something likeDo not ask me why but now seems to work with my initial solution. :)auto opDispatch(string name)() if(hasMember!(HeaderData,name){ readHeader(); return mixin("headerData." ~ name); }
Feb 10 2018