digitalmars.D.learn - alias fails to compile
- Arun Chandrasekaran (17/17) Apr 22 2019 What am I doing wrong here?
- Stefan Koch (6/23) Apr 22 2019 You are aliasing B.init.bb;
- Alex (45/70) Apr 22 2019 aa is a "static" semantic.
- Arun Chandrasekaran (56/130) Apr 22 2019 I see. This is a simplified use case in /usr/include/net/if.h on
- Adam D. Ruppe (2/2) Apr 22 2019 see this other thread for possible solutions too
- aliak (20/37) Apr 22 2019 You can get the behaviour you want with opDispatch, and
- Arun Chandrasekaran (2/44) Apr 22 2019 Works perfect for a single member! Not otherwise.
What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = B.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2
Apr 22 2019
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran wrote:What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = B.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2You are aliasing B.init.bb; You should try aliasing b.bb; though I am not sure in that'll work since alias is really only meant for symbols.
Apr 22 2019
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran wrote:What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = b.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2aa is a "static" semantic. your statement a.aa does not get translated in to a.b.bb like you think it does. You are doing nothing any different than A.aa = 4. which, when you realize this you'll understand the "need this" error. It's as if you are doingstruct A { union B { int bb; } B b; }alias aa = A.b.bb; aa = 4; which is like trying to say A.b.bb = 4; In D we can access "static" stuff using the object and so this can make things look like the mean something they are not. If you could magically pass the this to it using something like UFCS then it could work. import std.stdio; struct A { union B { int bb; } B b; alias aa = (ref typeof(this) a) { return &a.b; }; } void main() { A a = A(); a.b.bb = 4; a.aa(a).bb = 5; writeln(a.aa(a).bb); } But here a.aa is not aa(a) and UFCS does not work so one must first access the alias and then pass the this. Basically you are not going to get it to work. Just not how the semantics works. It would be nice if a.aa(a) could reduce to a.aa as is UFCS was smart enough to realize it could pass it to the alias as this, but it doesn't. else one might could do alias aa = this.b.bb; and a.aa = 4;
Apr 22 2019
On Monday, 22 April 2019 at 14:07:11 UTC, Alex wrote:On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran wrote:I see. This is a simplified use case in /usr/include/net/if.h on Linux, where struct ifreq has something like this: struct ifreq { union { char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */ } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short int ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[IFNAMSIZ]; /* Just fits the size */ char ifru_newname[IFNAMSIZ]; __caddr_t ifru_data; } ifr_ifru; }; p-p lnk */ address */ mask */ index */ */ _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0) _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0) Macro magic has been doing the job and luckily there is no ifr_name, etc defined in the user code. Looks like there is no way but to name the elements of this struct as ifr_name, etc?What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = b.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2aa is a "static" semantic. your statement a.aa does not get translated in to a.b.bb like you think it does. You are doing nothing any different than A.aa = 4. which, when you realize this you'll understand the "need this" error. It's as if you are doingstruct A { union B { int bb; } B b; }alias aa = A.b.bb; aa = 4; which is like trying to say A.b.bb = 4; In D we can access "static" stuff using the object and so this can make things look like the mean something they are not. If you could magically pass the this to it using something like UFCS then it could work. import std.stdio; struct A { union B { int bb; } B b; alias aa = (ref typeof(this) a) { return &a.b; }; } void main() { A a = A(); a.b.bb = 4; a.aa(a).bb = 5; writeln(a.aa(a).bb); } But here a.aa is not aa(a) and UFCS does not work so one must first access the alias and then pass the this. Basically you are not going to get it to work. Just not how the semantics works. It would be nice if a.aa(a) could reduce to a.aa as is UFCS was smart enough to realize it could pass it to the alias as this, but it doesn't. else one might could do alias aa = this.b.bb; and a.aa = 4;
Apr 22 2019
see this other thread for possible solutions too https://forum.dlang.org/post/anasidaotexgfkyiqmlj forum.dlang.org
Apr 22 2019
On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran wrote:What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = B.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2You can get the behaviour you want with opDispatch, and generalize it with a mixin template: mixin template AliasMember(string aliasName, string memberName) { ref opDispatch(string name)() if (name == aliasName) { return mixin(memberName); } } struct A { union B { int bb; } B b; mixin AliasMember!("aa", "b.bb"); } void main() { A a = A(); a.aa = 4; }
Apr 22 2019
On Monday, 22 April 2019 at 19:57:11 UTC, aliak wrote:On Monday, 22 April 2019 at 08:02:06 UTC, Arun Chandrasekaran wrote:Works perfect for a single member! Not otherwise.What am I doing wrong here? struct A { union B { int bb; } B b; alias aa = B.bb; } void main() { A a = A(); // a.b.bb = 4; // works a.aa = 4; // fails } https://run.dlang.io/is/kXaVy2You can get the behaviour you want with opDispatch, and generalize it with a mixin template: mixin template AliasMember(string aliasName, string memberName) { ref opDispatch(string name)() if (name == aliasName) { return mixin(memberName); } } struct A { union B { int bb; } B b; mixin AliasMember!("aa", "b.bb"); } void main() { A a = A(); a.aa = 4; }
Apr 22 2019