Can now use alias for collapsing multiple fields in nested structs

Mike Shah mshah.475 at gmail.com
Mon Dec 30 04:38:07 UTC 2024


On Sunday, 29 December 2024 at 21:08:39 UTC, Walter Bright wrote:
> On 12/29/2024 8:23 AM, Timon Gehr wrote:
>> Works in my frontend, does not work with DMD:
>
> Oh, how I wish you'd make PRs for DMD!

A nice expansion of alias!

I tested it out below with some more nesting to see if it worked 
-- and it did :)

```
import std.stdio;
import std.conv;

struct Type{
     struct Nested1{
         Nested2 s;
         struct Nested2{
             int t;
         }
     }

     // A field in our type for testing purposes
     Nested1 field;

     // alias to a struct member -- 'another name' to conveniently 
access
     // fields.
     // This is the main feature I am demonstrating
     // i.e. alias to a struct member.
     alias innerT = field.s.t;
}

void main(){

     // Create an instance of our type
     Type test;

     // Access member variable conveniently through alias.
     test.innerT= 42;

     // Assert we are accessing the correct value
     assert(test.field.s.t == 42);

     // Write out the result
     string value = i"$(test.field.s.t)".text;
     writeln(value);
}

```


More information about the Digitalmars-d mailing list