Can now use alias for collapsing multiple fields in nested structs

Timon Gehr timon.gehr at gmx.ch
Sun Dec 29 16:23:28 UTC 2024


On 12/28/24 20:21, Walter Bright wrote:
> It's commonplace in C and C++ to use something like:
> 
> ```
> #define ti t.i
> 
> s.ti
> ```
> as a shortcut for cutting through layers of struct declarations. Now 
> `alias` can do it, too!
> 
> This now works:
> 
> ```
> struct T {
>      int k,i = 2;
> }
> 
> struct S {
>      int x;
>      T t;
>      alias ti = t.i;
> }
> 
> void main() {
>      T t = T(1, 2);
>      S s;
>      assert(s.ti == 2);
> }
> ```
> ...

Neat! FWIW, this also works in my frontend. ;)

> `alias` has evolved from simply `typedef` into quite the multi-tool!
> ...

Yes. Now if only IFTI were able to see through templated aliases.

Works in my frontend, does not work with DMD:

```d
alias Slice2D(T)=T[][];

void foo(T)(Slice2D!T slice){
     pragma(msg, T);
}

void main(){
     foo([[1,2],[3,4]]); // int
}
```

> https://github.com/dlang/dmd/pull/20611
> 
> Manu suggested this.

Thanks!


More information about the Digitalmars-d mailing list