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¶
All types except capabilities MUST be allowed in structs. Function pointer types MUST be permitted as struct member types; unlike capabilities, function pointers are values that do not carry borrowed state.
The alloc expression MAY be used within structs for default initialization. When a struct is allocated, any alloc in its default values MUST use the parent struct's allocation region.
Member Defaults¶
All struct members MUST declare a default value. A member declaration without a default value MUST NOT be permitted.
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 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: it MUST NOT be stored in struct members, MUST NOT be returned from functions, and MUST NOT be stored in local variables.
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.