navid/haze / 48b013122efd104b9d16cfe4274727f898f9adfe2ce7378143f220c18de31b41 / 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
}