Can now use alias for collapsing multiple fields in nested structs
Nick Treleaven
nick at geany.org
Sat Jan 4 20:01:33 UTC 2025
On Thursday, 2 January 2025 at 21:14:25 UTC, ryuukk_ wrote:
> On Thursday, 2 January 2025 at 19:08:06 UTC, Walter Bright
> wrote:
>> On 1/2/2025 6:32 AM, Derek Fawcus wrote:
>>> 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?
>>
>> ```
>> struct outer {
>> int a;
>> struct {
>> int b;
>> int c;
>> }
>> int d;
>> }
>> ```
>
> that's hard to learn, why is it possible to do `outter.b = 42`?
That's how it works in C11. The spec shows some useful use cases:
https://dlang.org/spec/struct.html#anonymous
> ```
> struct outer {
> int a;
> struct {
> int b;
> int c;
> } inner;
> int d;
> }
> ```
>
> now it is easier to learn, it is an anonymous struct called
> `inner`
That complicates the parser, beginners would forget to type the
`;` after a struct definition and get confused at the parse
errors.
Why do you need an anonymous struct type there? Naming it and
using the struct name for `inner` isn't difficult, and it doesn't
seem needed that often.
More information about the Digitalmars-d
mailing list