Structs

A struct creates a nominal type with named fields. Two structs remain different types even when their fields match, unlike structural record systems.

Receiver methods use ordinary top-level function declarations in the same file as the type. Their first parameter shows shared or mutable access.

Runtime anonymous records are accepted deferred design. Their local hidden types keep nominal identity and use the shared MON construction notation.

Choose the explanation level: Struct declarations

Struct declarations

A struct groups named values into one new type.

Player = |
    name String,
    score I32,
|

Each field has a name and a type.

Fields can have defaults:

Player = |
    name String,
    score I32 = 0,
|

Player is its own type. Another struct with the same fields is still a different type.

A struct declares a nominal runtime type with named fields.

Vector2 = |
    x F64,
    y F64 = 0.0,
|

Contract for struct declarations

  • The declaration form is Name = | fields |.
  • Struct names use PascalCase.
  • Every field has a name and explicit type.
  • A field keeps its exact declared type. I32 and U32 remain distinct even when their physical widths match, as do Byte and U8.
  • Field names must be unique.
  • A field may provide = default.
  • Every field default must fully evaluate at compile time.
  • A field without a default is required during construction.
  • Matching field shapes do not make two structs interchangeable.
  • A transparent type alias does not create a new struct identity.

Field types remain concrete declaration facts. Constructors and defaults check values against those exact types; later uses never retag a field. Generic nominal shapes follow the bounded numeric-origin rules in Generics.

Point = |
    x I32,
    y I32,
|

Position = |
    x I32,
    y I32,
|

Point and Position remain different types.

Generic structs

Struct declarations may introduce declaration-site generic parameters.

Box type A = |
    value A,
|

Generic construction and inference are documented by Generics.

Choose the explanation level: Construction and fields

Construction and fields

Create a struct by calling its type name.

Player = |
    name String,
    score I32 = 0,
|

player = Player("Priya")

The constructor uses the default score.

Named fields make larger constructors easier to read:

player = Player(
    name = "Priya",
    score = 10
)

Read fields with a dot:

name = player.name

To change a field, the struct binding must be mutable:

player ~= Player("Priya")
~player.score = 10

A struct constructor uses the struct name and supplies its fields through MON syntax.

Point = |
    x I32,
    y I32 = 0,
|

first = Point(10)
second = Point(x = 10, y = 20)

Constructor routing

  • Positional arguments fill fields in declaration order.
  • Positional arguments must come before named arguments.
  • A named argument must match a real field.
  • Each field may be supplied once.
  • Omitted fields use their declared defaults.
  • Every field without a default is required.
  • Extra arguments are invalid.
  • Values are checked and contextually coerced against field types.

An exact field type can receive a raw numeric literal directly. An already typed value must match the field type; construction does not widen or retag it.

A constructor creates a fresh value.

Explicit nested construction

Construct a named child at its field's receiving boundary:

Size = | width I32, height I32 |
Window = | size Size |

window = Window(size = Size(width = 1280, height = 720))

An anonymous (width = 1280, height = 720) does not become a Size because the fields match. MON syntax adds no implicit nominal construction or structural conversion. Named type declarations remain separate, flat declarations.

MON data boundary

Source construction above is unchanged: an anonymous record never becomes a nominal struct because the fields match. MON literal struct data documents both anonymous (...) and one exact Name(...) qualifier by reference. An explicit qualifier is a checked assertion against the receiving schema, never implicit conversion and never user-code invocation. That contract is Rust-only serialised-data documentation; source behaviour is unchanged. See MON format.

Field reads

Use . to read a field.

point = Point(10, 20)
horizontal = point.x

A field read uses normal shared access.

Field writes

The root value must be a mutable place. Prefix the whole field target with ~.

point ~= Point(10, 20)
~point.x = 30

A field of an immutable binding cannot be assigned.

Field mutability is controlled by the root place and the current access. It is not a separate field type.

Const records use struct-shaped compile-time data but have additional restrictions owned by Constants and compile-time behaviour. Runtime anonymous records describes the separate deferred local hidden-struct feature.

Choose the explanation level: Runtime anonymous records

Runtime anonymous records

Accepted deferred implementation. Runtime anonymous records are planned and are not yet executable in the Alpha compiler.

An anonymous record groups local values without declaring a named type:

show_window ||:
    window ~= (
        title = "Strategy",
        size = (width = 1280, height = 720),
    )

    ~window.size.width = 1440
;

Every field needs a name and a value. Records can nest directly. Runtime fields may use ordinary runtime expressions and follow normal access and copy rules.

Each literal site has its own hidden nominal type. Matching fields do not make two different sites interchangeable. Use explicit constructors for named types.

The initial feature is local-only. Records carrying hidden types cannot be passed to functions, returned, exported or stored in collections or maps. Ordinary values extracted from their fields follow their normal rules.

See Const records for compile-time member groups and MON syntax for the shared notation.

Accepted deferred implementation. Runtime anonymous records are queued after shared MON syntax and numeric semantics. The examples below describe the accepted contract, not current executable support. See Progress.

A runtime anonymous record is a local value backed by a hidden nominal struct. It uses the named-only form of MON syntax. The ordinary struct owners provide construction, field access, copy, borrow validation and lifetime validation.

Construction

Size = |
    width I32,
    height I32,
|

show_window ||:
    window ~= (
        title = "Strategy",
        bounds = (
            size = Size(width = 1280, height = 720),
            visible = true,
        ),
    )

    ~window.bounds.size.width = 1440
    independent ~= copy window
    ~independent.title = "Overview"
;

A runtime receiving context selects a runtime record. Its field expressions need not fold. Every field has a unique name and an initializer. At least one field is required. Empty () is outside this runtime surface.

Field initializers run once in source order under ordinary expression, access and error rules. Labels introduce no local names. A mutable root permits field writes only when ordinary access rules allow them. It does not copy an existing child or grant additional authority over one.

Hidden nominal identity

Each static literal site owns one hidden nominal type within its compiled body. Repeated executions of that site produce values of the same type. Distinct sites remain different types even when their field names and types match. Aliases and copies preserve the type identity.

Compatible assignment requires the same resolved identity. Another literal of matching shape is not a replacement value of that type. The hidden type has no source-visible name, constructor name, receiver methods or trait conformance.

A named struct still uses its explicit constructor. An anonymous record does not satisfy a named field or parameter by matching its shape.

Recursive local composition

Anonymous parents may contain anonymous children directly or through an already bound local. They may also contain ordinary supported values, including explicitly constructed nominal structs and choices.

show_state ||:
    child = (count = 3)
    state = (
        saved = child,
        fresh = (enabled = true),
    )

    selected = state.fresh
    count = state.saved.count
;

A compiler-generated parent field carries the child's resolved hidden type. Nesting is supported recursively from the first runtime implementation. Extracting a child to another local preserves its type and alias relationship. An ordinary named or scalar leaf follows that leaf type's ordinary rules.

This composition permission applies to compiler-generated anonymous parent fields. It does not make hidden types legal in authored nominal field declarations or permit an unnamed child to bypass a named parent's declared field types.

Local-only uses

Supported uses are local binding, compatible assignment, projection, explicit copy and recursive composition inside anonymous parents. Normal type and access rules still apply.

A hidden identity cannot cross a function argument or return boundary, including private functions. It cannot appear in an authored signature, type alias, nominal struct or choice field, receiver method, trait evidence or exported value/interface. The initial implementation also rejects collection/map storage and generic arguments carrying a hidden identity.

These restrictions are transitive. A containing parent, optional or captured value cannot hide a prohibited identity. An ordinary leaf extracted from a record is not restricted merely because it came through that record.

A generic body may construct a local record from its resolved concrete values where ordinary compilation supports that body. Passing the hidden type as a generic argument remains invalid.

Values and capabilities

Records follow ordinary reference, copy, borrow and lifetime rules. Inline nesting creates no new region, implicit deep copy or escape exemption.

Compile-time records remain separate folded member groups, with the whole-value runtime restrictions in Const records. A runtime record never uses the compile-time record marker as its runtime type.

Wiring retains its own storage rules. MON construction does not turn wire identity or routes into ordinary storable field values. Runtime records gain no serialisation support from sharing MON syntax.

Choose the explanation level: Receiver methods

Receiver methods

A receiver method attaches behaviour to a struct or choice declared in the same file.

Its first parameter is named this:

Vector2 = |
    x F64,
    y F64,
|

length_squared |this Vector2| -> F64:
    return this.x * this.x + this.y * this.y
;

vector = Vector2(3.0, 4.0)
length = vector.length_squared()

Write the method as a normal top-level function in the same file as the type.

Call it with value.method(), not method(value).

This keeps method ownership visible without placing functions inside the type declaration. Only values of the receiver type can call the method.

A receiver method is a top-level function whose first parameter is named this.

Vector2 = |
    x F64,
    y F64,
|

length_squared |this Vector2| -> F64:
    return this.x * this.x + this.y * this.y
;

vector = Vector2(3.0, 4.0)
length = vector.length_squared()

Receiver declaration

  • this must be the first parameter.
  • A signature may contain exactly one this parameter.
  • this T is a shared receiver.
  • this ~T is a mutable receiver.
  • this is reserved for receiver declarations and the receiver method body.
  • A receiver method is called only with receiver syntax.
  • Calling it as method(value, ...) is invalid.
  • A receiver method may declare -> !. The contract is exact, like any other callable.

Supported receiver ownership

A source-authored receiver method belongs to a user-defined struct or choice declared in the same source file.

Aligned declaration-site generic nominal receivers are supported when the method belongs to that generic type declaration.

Source-authored receiver methods are rejected for:

  • builtin scalars
  • imported source types
  • dependency package types
  • external opaque types
  • nominal types declared in another file

Use a free function for a type owned elsewhere.

Compiler-owned collection operations and builder-owned builtin operations are not user extension methods.

Implicit failure follows the closed module boundary: an exported receiver method without an error slot must handle it locally, while a private method propagates it within the same module. See Errors.

Constants and receiver methods

A struct instance folded into a constant becomes a data-only const record. Its folded fields remain available under the constant rules, but runtime receiver methods are not available on the const record itself.

Use one projected field when runtime code needs the value. See Const records for the const-record contract.

Visibility

Receiver-method visibility follows receiver-type visibility.

  • A value carries its receiver type's usable methods into any file that holds it, without importing or binding the type's name.
  • Naming the type in an annotation, constructor, conformance or bound still needs an import or binding.
  • Methods are not imported, aliased or re-exported independently.
  • Namespace bindings may expose the receiver type, but its methods are not namespace fields.
  • A module public surface exposes same-file receiver methods when it exposes the receiver type.
  • A field and receiver method on the same type cannot use the same name.
Choose the explanation level: Mutable receivers

Mutable receivers

A method that changes its receiver uses this ~Type.

reset |this ~Vector2|:
    ~this.x = 0.0
    ~this.y = 0.0
;

The value must be stored in a mutable binding:

vector ~= Vector2(3.0, 4.0)
~vector.reset()

The first ~ makes the binding mutable. The second requests exclusive access for this method call.

A shared method is called without ~.

A mutable receiver method declares this ~Type.

Vector2 = |
    x F64,
    y F64,
|

reset |this ~Vector2|:
    ~this.x = 0.0
    ~this.y = 0.0
;

vector ~= Vector2(3.0, 4.0)
~vector.reset()

Contract for mutable receivers

  • The receiver root must be a mutable place.
  • The call uses ~receiver.method(...).
  • Omitting ~ for a mutable receiver method is invalid.
  • Adding ~ to a shared receiver method is invalid.
  • A temporary or computed rvalue cannot receive a mutable receiver call.
  • Field writes inside the method prefix the whole target, as in ~this.x = 0.0. Replacing this itself with ~this = is invalid.
  • ~ requests exclusive access for the call. It is not move syntax and is not part of type identity.

This differs from a fresh rvalue passed to a mutable free-function parameter. The compiler may materialise that free-function argument into a hidden local. A mutable receiver call still requires an authored mutable place.