navid/haze / ea08ceb46539df9cf3ac396bc0bd66fc4f6b9adc630e1596e79b8e4d3101673a / samples/baseline/07_records.hz

07_records.hz

← samples/baseline · 604 bytes · raw · download

// 07: records. Data is immutable: "changing" a value builds a new one.

type Point {
    x i32
    y i32
}

fn moved(p Point, dx i32, dy i32) Point => Point{ x = p.x + dx, y = p.y + dy }

fn add(a Point, b Point) Point => Point{ x = a.x + b.x, y = a.y + b.y }

fn dist2(p Point) i32 => p.x * p.x + p.y * p.y

fn main() {
    const p = Point{ x = 3, y = 4 }
    const q = moved(p, 1, 1)
    print(p.x)                    // 3   (p is unchanged)
    print(q.x)                    // 4
    print(q.y)                    // 5
    print(dist2(p))               // 25
    print(add(p, q).y)            // 9
}