fat struct style guide?

Kapendev alexandroskapretsos at gmail.com
Tue Feb 24 22:44:28 UTC 2026


On Tuesday, 24 February 2026 at 22:27:32 UTC, monkyyy wrote:
> On Tuesday, 24 February 2026 at 22:10:08 UTC, Kapendev wrote:
>> On Tuesday, 24 February 2026 at 21:56:47 UTC, monkyyy wrote:
>>> [...]
>>
>> ```d
>> // Tagged union.
>> struct Base { int x, y, w, h; ubyte type; }
>> struct Foo { Base base; int hp; }
>> struct Goo { Base base; string name; }
>> union Entity1 { Base base; Foo foo; Goo goo; }
>>
>> // MEGA struct.
>> struct Entity2 {
>>     int x, y, w, h;
>>     int hp;
>>     string name;
>>     ubyte type;
>> }
>>
>> void main() {
>>     auto e1 = Entity1();
>>     e1.base.x += 1; // Can just do that without checking or 
>> void magic.
>>     auto e2 = Entity2();
>>     e2.x += 2;      // It's the same thing.
>>
>>     import std.stdio;
>>     writeln("Entity1 size: ", e1.sizeof);
>>     writeln("Entity2 size: ", e2.sizeof);
>> }
>> ```
>
> Thats a compromise between fat structs and components, 
> everywhere you have base it acts fat

The point is that it's the same stuff, but with less fat.
There is only one thing mega sturcts do better and that is you 
not needing functions like:

```d
// Ensure at compile time that all types in the union have the 
same base.
static assert(Entity.isBaseAliasingSafe);
// Helpers for converting normal types to entity types.
static foreach (T; Entity.Types) {
     Entity xx(T value) => Entity(value);
}
```


More information about the Digitalmars-d-learn mailing list