navid/haze / 29e7ef5e690b244d506a169b7a4f892c737395b40bafffaedbaf7b650bf94bb8 / samples/baseline_2.8/generic_sums.hz
generic_sums.hz
← samples/baseline_2.8 · 784 bytes · raw · download
// Generic sum types, constructor inference, and recursive instances.
type Maybe[T] = | Nothing | Just(value T)
type Tree[T] =
| Leaf
| Node(left Tree[T], value T, right Tree[T])
fn show_i(value Maybe[i32]) i32 => match value {
Nothing => -1
Just(v) => v
}
fn show_s(value Maybe[str]) str => match value {
Nothing => "none"
Just(v) => v
}
fn sum(tree Tree[i32]) i32 => match tree {
Leaf => 0
Node(left, value, right) => sum(left) + value + sum(right)
}
fn main() {
const a = Just(7)
print(show_i(a))
const b Maybe[str] = Just("hello")
print(show_s(b))
const c Maybe[i32] = Nothing
print(show_i(c))
if a is Just(v) {
print(v)
}
const tree = Node(Leaf, 2, Node(Leaf, 3, Leaf))
print(sum(tree))
}