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..b includes b, a..<b leaves it out, and a range whose start is above its end runs no turn.
  • Walk a text with text.chars() or text.split(on: s), never the text itself.
  • The range is fixed before the first turn, and every turn runs: there is no break and no while.
  • Declare an accumulator before its loop with let edit, at the value for no turn, and change it by assignment.
  • A plain let and 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

Next:10 Collections