On Saturday, 28 December 2024 at 19:21:43 UTC, Walter Bright wrote:
>struct S {
int x;
T t;
alias ti = t.i;
}
Thank you, I'm curious what does this enable on top of
@property ref ti() => t.i;
?
December 31 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Sounds like you've done a lot of nice work with it! |
December 31 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 12/30/2024 6:45 AM, Steven Schveighoffer wrote:
> https://github.com/dlang/dmd/issues/17760
Manu wasn't the only one pushing hard for move constructors.
|
December 31 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 12/31/24 19:24, Walter Bright wrote:
> On 12/30/2024 2:22 PM, Timon Gehr wrote:
>> On 12/29/24 22:08, 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!
>>>
>>
>> Me too. I created that frontend back when I was a student with a lot of spare time.
>
> Is your frontend based on dmd or is it ground up?
>
It is ground up, it was the first bigger project I started with D. (First did a lexer, then an almost complete parser, then started working on semantic analysis and CTFE.)
|
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to ryuukk_ | On 12/30/2024 4:08 AM, ryuukk_ wrote:
> ```D
> struct Tada {
> struct {
> int a;
> int b;
> } data;
> };
>
>
> Tada tada;
> tada.data.a = 42;
> ```
>
> but D can't do that
That's deliberate, not a shortcoming.
There's no purpose to writing declarations that way, unless you're using C.
|
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Fawcus | On 12/30/2024 9:09 AM, Derek Fawcus wrote: > Whereas this new D enhancement seems to be somewhat different, as in D it seems the structs always need to have tags. Not exactly. Anonymous structs in D are used to simplify layout, as in: ``` struct S { int a; union { struct { int j, k; } double d; } } ... S.a S.j S.k S.d ``` No extra tags needed. and: ``` struct S { struct { int a, b; } } ... S.a S.b ``` The anonymous struct there is pointless. > Not that I'm arguing against such an enhancement; simply that the C limitation, and preprocesser games being used as a partial justification is long obsolete. The C11 thing is just copying what D did. |
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 12/31/2024 11:08 AM, Walter Bright wrote:
> On 12/30/2024 6:45 AM, Steven Schveighoffer wrote:
>> https://github.com/dlang/dmd/issues/17760
>
> Manu wasn't the only one pushing hard for move constructors.
>
Ack, I got two threads mixed up.
Yes, Steven, it looks like you asked for it first!
|
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 02/01/2025 9:16 PM, Walter Bright wrote:
> On 12/30/2024 4:08 AM, ryuukk_ wrote:
>> ```D
>> struct Tada {
>> struct {
>> int a;
>> int b;
>> } data;
>> };
>>
>>
>> Tada tada;
>> tada.data.a = 42;
>> ```
>>
>> but D can't do that
>
> That's deliberate, not a shortcoming.
>
> There's no purpose to writing declarations that way, unless you're using C.
Or writing a binding to C.
Or needing to group some fields together and not have names overlap in a union. Useful when writing a sum type. Such as an AST.
Or wanting to guarantee a specific bitfield layout... (perhaps matching C!)
I'd love to have this and would use it quite frequently.
Its a feature that once you learn that it exists, but not in D, you long for it, even if it isn't worth making noise over.
|
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Saturday, 28 December 2024 at 19:21:43 UTC, Walter Bright wrote: >
Thank you, I'm curious what does this enable on top of
? |
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Well I have seen this used in C: ```C #define struct_group(NAME, MEMBERS...) \ union { \ struct { MEMBERS }; \ struct { MEMBERS } NAME; \ } struct outer { int a; struct_group(inner, int b; int c; ); int d; }; ``` specifically to allow access to the members as if they were fields, as well as allowing access to a subset as a group. (dmd can cope with that via ImportC, ldc pukes on the preprocessor games) The closest native equivalent in D, would seem to be the following, making use of the new facility. ```D struct outer { int a; struct inner_ { int b; int c; } inner_ inner; int d; alias b = inner.b; alias c = inner.c; }; ``` Or is there a nicer way to write that in D? |
January 02 Re: Can now use alias for collapsing multiple fields in nested structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On 1/2/2025 3:32 AM, Vladimir Panteleev wrote:
> Thank you, I'm curious what does this enable on top of
>
> ```d
> @property ref ti() => t.i;
> ```
> ?
Great question! Some answers:
1. the alias means `i` can be any symbol, not just a field
2. the alias thing has been brought up before (see Steven) yet nobody has thought of your solution
3. people simply expect the alias to work
4. use of @property has been discouraged for a long time
5. the alias is simpler and easier on the eyes
|