Skip to content

Struct Declarations

Overview

Struct declarations define a named composite data type. Structs are exclusively accessed through handles and capabilities; raw struct values MUST NOT be directly accessible.

Syntax

struct Point;

struct Point {
  x = 0;
  y = 0;
}

struct Vector {
  function magnitude(): float {
    return 0.0;
  }
}

Rules

Member Types

Primitive types, function pointer types, struct handles, and slice handles MUST be permitted as struct member field types. Capability types MUST NOT be permitted as struct member field types. Raw struct value types MUST NOT be permitted as struct member field types.

The alloc expression MAY be used within struct member field defaults for default initialization. When a struct instance is allocated, any nested alloc in its field defaults MUST use the containing allocation's region.

Member Defaults

All struct member data fields MUST declare a default value. A field declaration without a default value MUST NOT be permitted.

When a struct instance is allocated, field defaults MUST be evaluated in declaration order.

Default initialization MUST be finite. A default value expression that would recursively re-enter the same default initialization chain MUST be rejected as a compile-time error.

Member Uniqueness

Duplicate member names within a struct declaration MUST NOT be permitted. Member fields and member functions MUST NOT share names.

Recursive Constraints

Recursive structs MUST be permitted.

Member Functions

Member functions MUST follow the same declaration rules as top-level function declarations. Member functions MUST have an implicit first parameter of a capability type for the enclosing struct. This parameter MUST be accessible within the function body via the this identifier. The type of this MUST be read StructType or write StructType, where StructType is the enclosing struct type.

Member functions MAY be region-qualified using ^ function. A region-qualified member function MUST follow the same active-region call-site requirement and return-lifetime constraints as region-qualified top-level functions.

Member function names MUST be scoped by their enclosing struct. The same member function name MAY be used in different structs. Member function symbol mangling MUST use only the enclosing struct name and the member function name.

Member Function Visibility

Member functions MUST follow the same export rules as top-level function declarations. A member function MUST be individually marked with the export qualifier to be visible to other compilation units. A member function MUST NOT be permitted to be exported if its enclosing struct is not also exported.

Member Function Capability Selection

The compiler MUST analyze the body of each member function to determine whether the struct is modified. A member function MUST use read StructType for this if the compiler can guarantee that the function body does not modify any field of the struct. Otherwise, write StructType MUST be used. A function body modifies the struct if it assigns to any field via this (e.g., this.field = expr), passes this where a write capability is required, calls another member function on this whose implicit this resolves to write, or moves a handle member via this.

Extern Member Functions

For extern member functions, a write capability MUST always be used for the implicit first parameter.

This Usage

Inside a member function body, this MUST be used to access fields (this.fieldName) and call other member functions (this.otherMethod()). The this identifier MUST NOT be omitted; implicit field or method access MUST NOT be permitted.

Because this is a capability type, it MUST obey all capability rules.

Forward Declarations

A forward-declared struct (struct Point;) MUST permit handle declarations of that type before the full definition is provided. Operations requiring the struct's layout MUST NOT be permitted on a forward-declared struct. The alloc expression MUST NOT be used with a forward-declared struct type. The full definition MUST appear in the same compilation unit.

Visibility

The export qualifier MUST be supported for struct declarations to make them visible to other compilation units. Structs without the export qualifier MUST NOT be externally visible.