# Structs

Structs group named fields into a value. They are the main record type in Vex. Behavior is declared outside the struct with receiver methods.

## Define and construct a struct

~~~vex
struct Point {
    public:
    x: i32,
    y: i32,
}

fn main(): i32 {
    let point = Point { x: 3, y: 4 };
    $println(point.x + point.y);
    return 0;
}
~~~

Field declarations are grouped under visibility sections. Use public for fields that callers may read or write, readonly for fields that callers may read but not write, and private for module-internal fields. When a struct is part of a module API, export the struct declaration as well.

~~~text
export struct Account {
    private:
    internal_id: i64,

    readonly:
    balance: f64,

    public:
    name: string,
}
~~~

Keep the visibility choice close to the type definition. It is part of the data model, not a comment about how the type happens to be used today.

## Methods

Methods use an external receiver:

~~~vex
struct Rectangle {
    public:
    width: i32,
    height: i32,
}

fn (rectangle: &Rectangle) area(): i32 {
    return rectangle.width * rectangle.height;
}

fn main(): i32 {
    let rectangle = Rectangle { width: 5, height: 7 };
    $println(rectangle.area());
    return 0;
}
~~~

The receiver is an ordinary typed parameter inside parentheses. A call such as rectangle.area() supplies that parameter automatically.

A mutable receiver is written with !:

~~~text
fn (rectangle: &Rectangle!) resize(width: i32, height: i32) {
    rectangle.width = width
    rectangle.height = height
}
~~~

Mutation through a receiver requires a mutable binding at the call site and an exclusive mutable borrow.

## Associated functions

An associated function is qualified by the type name and has no receiver:

~~~vex
struct Point {
    public:
    x: i32,
    y: i32,
}

fn Point.origin(): Point {
    return Point { x: 0, y: 0 };
}

fn main(): i32 {
    let point = Point.origin();
    $println(point.x + point.y);
    return 0;
}
~~~

Use associated functions for constructors and type-level operations. Use receiver methods when an existing value is the subject of the operation.

## Callable constructors

A function can share its name with its result type. Constructor overloads are
ordinary source-defined functions; their bodies supply initialization policy:

~~~vex
struct Counter { value: i64 }

fn Counter(): Counter { return Counter { value: 73 }; }
fn Counter(value: i64): Counter { return Counter { value: value + 1 }; }

fn main(): i32 {
    let first = Counter();
    let second = Counter(40);
    return if first.value == 73 && second.value == 41 { 0 } else { 1 };
}
~~~

Overload selection and generic arguments are resolved before code generation.
The backend does not replace a selected constructor with zero-initialization
because a type is registered in the Prelude. `Vec<T>()` and `Channel<T>(n)` use
their Vex constructor bodies. Box construction remains integrated with VUMM's
compiler-managed ownership strategy.

## Generic structs

Structs may have type parameters:

~~~vex
struct Container<T> {
    public:
    value: T,
}

fn main(): i32 {
    let container = Container<i32> { value: 42 };
    $println(container.value);
    return 0;
}
~~~

A generic owner carries its concrete type arguments into its fields and
methods. `Container<i32>` and `Container<string>` therefore have different
payload ownership requirements. See [Generics](generics.md).

## Structs and ownership

A struct owns its fields unless a field is a reference or another explicitly borrowed type. Passing a struct by value may move it:

~~~text
fn consume(point: Point) { ... }
fn inspect(point: &Point) { ... }
~~~

Use an immutable reference when the callee only needs to read the value. Use a mutable reference when it must update the value. See [Ownership](../memory/ownership.md) and [Borrowing](../memory/borrowing.md) for the rules that govern these calls.

## Structs and contracts

Contracts describe required methods and can be attached to a struct declaration:

~~~text
contract Area {
    area(): i32;
}

struct Rectangle: Area {
    public:
    width: i32,
    height: i32,
}
~~~

This declaration fragment also requires an `area` receiver implementation.
See [Contracts](contracts.md) for a complete conformance example.

## Guidance

- Keep data representation and behavior easy to find.
- Make fields public only when callers should depend on direct field access.
- Prefer associated functions for construction instead of exposing an invalid intermediate state.
- Use references for read-only inspection and avoid unnecessary moves.
- Give constructors responsibility for establishing the type's invariants.

## Next steps

- [Enums](enums.md)
- [Tuples](tuples.md)
- [Generics](generics.md)
- [Contracts](contracts.md)
- [Ownership](../memory/ownership.md)
