E1062: guard needs more than read-only access

View source

A guard is written Pattern if condition => body.

A guard is written Pattern if condition => body. It decides whether a branch applies, so it may only look at values (ADR-0016). It cannot move or change a resource while it decides. A guard may call a verb whose parameters are all abs, because such a call only reads. The compiler rejects a guard that calls any other verb (one with an erg, dat or ins parameter), builds a struct, indexes an array or contains a case or an if, and a guard that reads a binding that is no longer live. The message names the call, the name or the construct, and states the rule: a guard may read bindings and call verbs whose parameters are all abs; bind anything else before the case. The error points at it.

Example

actus
verb positive(erg n: Int) -> Bool {
    return n > 0;
}

enum Shape {
    Circle(Int),
    Dot,
}

verb area(abs s: Shape) -> Int {
    return case s {
        Shape.Circle(r) if positive(n: r) => 1,
        Shape.Circle(r) => 2,
        Shape.Dot => 3,
    };
}

verb main() {
}

Fix

Call only a verb whose parameters are all abs, write the test with operators on bindings and literals, or compute it before the case and name it with a binding.

actus
verb positive(abs n: Int) -> Bool {
    return n > 0;
}

enum Shape {
    Circle(Int),
    Dot,
}

verb area(abs s: Shape) -> Int {
    return case s {
        Shape.Circle(r) if positive(n: r) => 1,
        Shape.Circle(r) => 2,
        Shape.Dot => 3,
    };
}

verb main() {
}

Run actus explain E1062 in your terminal to read this entry offline. Browse Ownership & types or all error codes.