Skip to content

09 · Data Structures

Contextual construction

When an exact struct type is expected, .{ fields } constructs that struct without repeating its name. Field defaults, privacy and completeness still apply. The compiler never guesses a nominal identity from field names.

On this page

When an exact struct type is expected, .{ fields } constructs that struct without repeating its name. Field defaults, privacy and completeness still apply. The compiler never guesses a nominal identity from field names.

Complete example

Contextual construction.

22-contextual-structs.zolo
Playground
// A known nominal context supplies the construction name.

struct Point { x: int = 0, y: int = 0 }
struct Segment { start: Point, end: Point }
fn origin() -> Point { .{} }
fn magnitude(point: Point) -> int { point.x + point.y }

fn main() {
    let x = 3
    let point: Point = .{ x, y: 4 }
    print(magnitude(.{ x: 5 })) // expected: 5

    print(point.y) // expected: 4

    let line: Segment = .{ start: .{ x: 1 }, end: .{ x: 7, y: 2 } }
    print(line.end.x) // expected: 7

    let shifted: Point = .{ x: point.x + 10, ..point }
    print(shifted.x) // expected: 13

}

Rules and behavior

Records, maps and updates · Documentation

From a checkout of the repository:

zolo examples/features/06-data-structures/22-contextual-structs.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br