navid/haze / 48ef02229ff0b40a4849e26fc1a9f371991f791d48bb426ffeb35dad068eb87d / samples/baseline_2.8/generic_records.hz

generic_records.hz

← samples/baseline_2.8 · 738 bytes · raw · download

// Generic record declarations, nesting, inference, and mutation.

type Pair[A, B] {
    first A
    second B
}

type Box[T] {
    value T
}

fn first[A, B](p Pair[A, B]) A => p.first

fn main() {
    const p = Pair[i32, str]{ first = 1, second = "one" }
    print(first(p))
    print(p.second)

    const inner = Pair[i32, str]{ first = 2, second = "two" }
    const outer = Pair[Pair[i32, str], bool]{ first = inner, second = true }
    print(outer.first.first)
    print(outer.first.second)
    print(outer.second)

    var box = Box[i32]{ value = 5 }
    box.value = 9
    print(box.value)

    var boxes = [Box[i32]{ value = 10 }, Box[i32]{ value = 20 }]
    boxes[1].value = 30
    print(boxes[0].value)
    print(boxes[1].value)
}