Can now use alias for collapsing multiple fields in nested structs

Derek Fawcus dfawcus+dlang at employees.org
Thu Jan 2 14:32:39 UTC 2025


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?


More information about the Digitalmars-d mailing list