← All examples

Check tiers

check_tiers.zg
fn draw ['y': bin16] { std::print.useful["drawing ", 'y', newln]; }
MAIN {
    # A guardrail the compiler can see: past the `if`, the value is inside bin16's range,
    # so the narrowing below is proven and `check` says so. A name never changes numeric
    # family (`types.md`), so the value is a bin from the start — `to.bin16` would be the
    # way to cross from an integer, and it would fail rather than round silently.
    let 'x': bin32 = 70000.0;
    if 'x' > 65_504.0 {
        std::print.stored["too big", newln];
    } else {
        check let 'y': bin16 = 'x';
        draw['y'];
    }
    let 'xs' := [1, 2, 3];
    check {
        for 'i' from 1 to list.length[lend 'xs'] by 1 {
            std::print.useful['xs'[pos 'i'], " "];
        }
    }
    std::print.stored[newln];
    no_check change 'xs'[pos 2] = 9;
    # crossing families is spelled, and it fails rather than rounds (`types.md`)
    check if 'x' > 0.0 { change 'xs'[pos 1] = to.int32['x' div.near 2.0] otherwise 0; }
    std::print.useful['xs', newln];
}
Output
too big
1 2 3 
[35000, 9, 3]