fat struct style guide?
Kapendev
alexandroskapretsos at gmail.com
Tue Feb 24 22:10:08 UTC 2026
On Tuesday, 24 February 2026 at 21:56:47 UTC, monkyyy wrote:
>> You get the same type of indirection with a "mega struct" if
>> we are talking about type checking.
>> And you will be checking types often in a game.
>> There is literally no difference in how a tagged union and a
>> mega struct works if you create a union with members that all
>> share the same first field.
>> The only difference will be the size, with tagged unions being
>> smaller and faster to iterate over.
>
> only with a pretty void* cast is accessing a tagged union field
> that happens to line up 0 indirection, everyone here is going
> to tell me to do the safe then and write an abstracted getter
> that gets the .offsetof `age`.
```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);
}
```
More information about the Digitalmars-d-learn
mailing list