Struct / Union types

Naming

User Type names must always start with a capital letter (A-Z) and be less than 32 characters in length. Valid non-start characters are: [A-Z a-z 0-9 _]

Syntax

<public> type [Name] struct/union <attributes> {
    [members]
}

Members / Sub-structs

Member syntax for sub-structs is:

struct/union <name> {
    [members]
}

Member syntax for non-substructs is:

[type specification] [name] ;

Rules:

  • member types may not be const
  • member names adhere to VarDecl naming rules
  • substructs may be anonymous (no name)
  • if substructs are named, the name has the same rules as other members
  • member names must be unique, but multiple sub-structs have the same name if at least one of the sub-structs itself is named.

Attributes

Structs/Unions can have the following attributes:

  • export
  • packed (structs only)
  • unused
  • aligned
  • opaque

Examples

public type Point struct @(packed, aligned=8) {
    i32 x;
    i32 y;
}

type Outer struct {
    struct inner {
        i32 x;
    }
    Other o;
    union {
        void* p;
        u32* u;
        bool* b;
    }
}