DIP ID | XXXX |
---|---|
Title | Named Anonymous Structs |
Author | ryuukk |
Status | Draft |
Created | 2025-04-08 |
Last Updated | 2025-04-08 |
Implemented | No |
This DIP proposes allowing named fields to be declared using anonymous struct types in a single inline syntax. The feature enhances readability and code brevity by allowing constructs like:
struct Data {
struct { int data; } inner;
}
This combines the declaration of an anonymous struct and the assignment of a named field, without the need to define a separate named type.
Currently, to define a struct field that contains a small group of related values, one must either define a separate struct type:
struct Inner { int data; }
struct Data { Inner inner; }
or use a verbose inline struct:
struct Data {
struct Inner {
int data;
} Inner inner;
}
This proposal simplifies this into a more intuitive and concise form:
struct Data {
struct { int data; } inner;
}
This is especially useful for:
- Grouping fields semantically
- Reducing type namespace clutter
- Improving struct layout and clarity without sacrificing usability
The proposed syntax allows defining an anonymous struct
type directly as part of a field declaration, with a name for the field:
struct Container {
struct { int x; int y; } position;
struct { string name; int id; } metadata;
}
Each field (position
, metadata
, etc.) holds an instance of an internally generated, compiler-defined anonymous struct.
- Each inline
struct { ... } name;
defines an anonymous struct type and a named field that holds it. - The anonymous struct type is not accessible via a type name.
- The field can be accessed normally:
c.position.x
,c.metadata.name
, etc. - The compiler may internally generate a unique type name for each anonymous struct, but this name is not exposed to user code.
Extend the aggregate member declaration syntax to support inline anonymous struct field declarations:
AggregateMember:
...
StructField
StructField:
'struct' '{' StructBodyDeclaration* '}' Identifier ';'
struct Event {
struct { int day; int month; int year; } date;
}
Event e;
e.date.day = 8;
struct Packet {
struct { int id; string tag; } header;
ubyte[] payload;
}
struct Config {
struct { bool fullscreen; int resolution; } display;
struct { string path; bool autoSave; } fileSettings;
}
struct Character {
string name;
int level;
struct {
int hp;
int mp;
int stamina;
} stats;
struct {
struct { int x; int y; int z; } position;
struct { float yaw; float pitch; float roll; } rotation;
} transform;
struct {
struct {
string type;
int damage;
float cooldown;
} weapon;
struct {
string name;
int armorRating;
} armor;
} equipment;
struct {
bool isNPC;
string faction;
} meta;
}
void main() {
Character hero;
hero.name = "MyHero";
hero.level = 5;
hero.stats.hp = 100;
hero.stats.mp = 100;
hero.stats.stamina = 100;
hero.transform.position = { 5, 0, 5 };
hero.transform.rotation.yaw = 90.0f;
hero.equipment.weapon = { "Sword", 25, 1.5f };
hero.equipment.armor.name = "Steel Armor";
hero.equipment.armor.armorRating = 40;
hero.meta.isNPC = false;
hero.meta.faction = "Guardians";
}
struct Data {
struct Field {
int value;
} Field field;
}
struct Field { int value; }
struct Data { Field field; }
Both require more boilerplate and additional names in the type system.
None. This is a purely additive syntax that does not affect existing code.
- Anonymous struct types must be unique per field but internal to the parent type.
- Debuggers and reflection should show full field names and inner structure clearly.
__traits
likeallMembers
andfieldTypeTuple
should treat these as regular struct fields.
This proposal introduces a concise, readable way to declare named fields with anonymous struct types. It enhances D’s expressive power and reduces unnecessary verbosity in aggregate definitions, especially for small or throwaway structures.
This document is released into the public domain.