Skip to content

Instantly share code, notes, and snippets.

@ryuukk
Last active April 8, 2025 17:39
Show Gist options
  • Save ryuukk/f264ec9afcce87efacbe284f13bdb2be to your computer and use it in GitHub Desktop.
Save ryuukk/f264ec9afcce87efacbe284f13bdb2be to your computer and use it in GitHub Desktop.

DIP: Named Anonymous Structs

DIP ID XXXX
Title Named Anonymous Structs
Author ryuukk
Status Draft
Created 2025-04-08
Last Updated 2025-04-08
Implemented No

Abstract

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.


Motivation

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

Description

New Syntax

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.

Semantics

  • 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.

Grammar Changes

Extend the aggregate member declaration syntax to support inline anonymous struct field declarations:

EBNF

AggregateMember:
    ...
    StructField

StructField:
    'struct' '{' StructBodyDeclaration* '}' Identifier ';'

Use Cases

Example 1: Nested Values

struct Event {
    struct { int day; int month; int year; } date;
}
Event e;
e.date.day = 8;

Example 2: Grouped Metadata

struct Packet {
    struct { int id; string tag; } header;
    ubyte[] payload;
}

Example 3: Configuration Grouping

struct Config {
    struct { bool fullscreen; int resolution; } display;
    struct { string path; bool autoSave; } fileSettings;
}

Example 4: Game Character System

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";
}

Alternatives

Current Verbose Syntax

struct Data {
    struct Field {
        int value;
    } Field field;
}

Named Type Declaration

struct Field { int value; }
struct Data { Field field; }

Both require more boilerplate and additional names in the type system.


Breaking Changes

None. This is a purely additive syntax that does not affect existing code.


Implementation Considerations

  • 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 like allMembers and fieldTypeTuple should treat these as regular struct fields.

Summary

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.


Copyright

This document is released into the public domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment