Jump to page: 1 25  
Page
Thread overview
December 28
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);
}
```

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

https://github.com/dlang/dmd/pull/20611

Manu suggested this.
December 28
On Saturday, 28 December 2024 at 19:21:43 UTC, 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);
> }
> ```
>
> `alias` has evolved from simply `typedef` into quite the multi-tool!
>
> https://github.com/dlang/dmd/pull/20611
>
> Manu suggested this.

Amazing!
December 28
On Saturday, 28 December 2024 at 19:21:43 UTC, Walter Bright wrote:

>
> Manu suggested this.

People discussed this for ages.
December 28

On Saturday, 28 December 2024 at 19:21:43 UTC, Walter Bright wrote:

>

struct S {
int x;
T t;
alias ti = t.i;
}

void main() {
T t = T(1, 2);
S s;
assert(s.ti == 2);
}

Thanks. I've wanted to do this for a long time.

December 29
On Saturday, 28 December 2024 at 19:21:43 UTC, 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.

It was... That particular preprocessor game largely became unnecessary once we gained C11 anonymous structs and unions.  Which is a good thing, as it can occasionally induce some nasty effects.

I've not had to use it in newly designed things since then. I've only had to use it when interacting with old code.

DF
December 28
On 12/28/2024 12:01 PM, bauss wrote:
> Amazing!

It rocked my world, too!
December 28
Sorry, but I missed it or it wasn't in the forums.
December 28
On 12/28/2024 5:23 PM, Derek Fawcus wrote:
> It was... That particular preprocessor game largely became unnecessary once we gained C11 anonymous structs and unions.

D had anonymous structs and unions long before C11.

BTW, I did find an appropriate use for it in the backend, unfortunately I can't use it because the bootstrap compiler is old old old.

December 29
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!
December 29
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!

« First   ‹ Prev
1 2 3 4 5