Home / Learn / 9 Repeating: for
Release v0.4.0Edition 0.2
9.8 Recap and reference
Page 9 of 9
9.8.1 For agents
una --json check . prints each diagnostic with its code, message, primary span and repairs; U5001 also carries a counterexample. U2019 has no repair: its message names the declaration, let edit x, or, for a loop variable, which is read-only, a copy bound in the body, let edit next_x = x. U5001 and U5002 rank repairs without edits, from a guard (escape_cost 0) to checked (1). The compact rule: for walks a..b (end included), a..<b (end left out), t.chars() or t.split(on: s), fixed before the first turn; a name a loop changes is let edit, declared before it, with a refined type when the code after the loop needs its fact.
9.8.2 Recap
a..bincludesb,a..<bleaves it out, and a range whose start is above its end runs no turn.- Walk a text with
text.chars()ortext.split(on: s), never the text itself. - The range is fixed before the first turn, and every turn runs: there is no
breakand nowhile. - Declare an accumulator before its loop with
let edit, at the value for no turn, and change it by assignment. - A plain
letand the loop variable are never assigned, and no name shadows another. - The verifier checks a body once for any turn, and keeps only the refined types of the names the loop changes.
- Give an accumulator a refined type,
let edit total: Units = 0, when the code after the loop needs its fact. - With no limit known in advance, use recursion with a case that stops it.
9.8.3 Reference
- Construct pages:
for,let edit, refined kinds andwhere,requires, what the verifier proves,example - Standard library: the prelude (
is_digits,trim), text (chars,split,slice) - Diagnostics: U0101, U0103, U1002, U2001, U2019, U5001, U5002
- Specification: memory model: editable bindings and loops, verification, text, standard library
Next:10 Collections