alias restriction??!

Carl Sturtivant sturtivant at gmail.com
Sat Jul 18 18:46:16 UTC 2020


Here's a toy version of a problem in the wild.

struct S {
     long first;
     union T {
         long one;
         double two;
     }
     T second;
     alias First = first;
     alias Second = second.one;
}

void main() {
     S x;
     x.First = 4;
     x.Second = 5; // compilation error: "Error: need this for one 
of type long"
}

Second is a fixed offset into an S and it would be nice to have a 
name for it. Why doesn't alias know it's OK?

I can work around this with some duplication as follows.

struct S {
     long first;
     union {
         long one;
         double two;
         union T {
             long one;
             double two;
         }
         T second;
     }
     alias First = first;
     alias Second = one;
}

void main() {
     S x;
     x.First = 4;
     x.Second = 5;
     x.second.one = 6;
     import std.stdio;
     writeln(x);
     writeln(x.one);
     writeln(x.Second);
}

Is there any way to avoid the duplication of the entries in the 
anonymous union, aside from using a mixin template?

Is there some other way (not involving writing a function) to 
work around this?



More information about the Digitalmars-d-learn mailing list