Home / Learn / 16 Memory

Release v0.4.0Edition 0.2

16.7 Common mistakes

Page 8 of 10

Each mistake below is a small change to day-to-day/src/stockcount.una, followed by what una check . reports.

16.7.1 Leaving out edit at the call (U2020)

Write apply(ledger, …) in close_day:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -62,3 +62,3 @@
   for movement in day.movements
-    let line = apply(edit ledger, movement: movement)
+    let line = apply(ledger, movement: movement)
       if error use "rejected: {reason(error)}"
Diagnostic
[U2020] Error: parameter `ledger` of `apply` is declared `edit`, so its argument is written `edit ledger`
    ╭─[ src/stockcount.una:63:22 ]
    │
 63 │     let line = apply(ledger, movement: movement)
    │                      ───┬──
    │                         ╰──── parameter `ledger` of `apply` is declared `edit`, so its argument is written `edit ledger`
    │
    │ Help: repair 1: write `edit ledger`; edit src/stockcount.una:63:22..63:28 to "edit ledger"
    │
    │ Note: run `una explain U2020` for an explanation
────╯
1 of 1 diagnostics have an edit that `una fix` applies (`una fix --dry-run` lists them)

Why: the call must show which bindings it may change, so a reader never has to open apply to find out. The mark points at the argument that lacks the word.

Fix: write edit ledger. The repair carries the edit, and una fix . applies it.

See U2020.

16.7.2 Passing a plain let as edit (U2019)

Declare the ledger with let instead of let edit:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -59,3 +59,3 @@
 =
-  let edit ledger = Ledger(site: day.site, counts: day.opening, applied: 0)
+  let ledger = Ledger(site: day.site, counts: day.opening, applied: 0)
   let edit lines: List[Text] = []
Diagnostic
[U2019] Error: `ledger` is not editable; declare it `let edit ledger`, or the parameter `edit ledger`
    ╭─[ src/stockcount.una:63:27 ]
    │
 63 │     let line = apply(edit ledger, movement: movement)
    │                           ───┬──
    │                              ╰──── `ledger` is not editable; declare it `let edit ledger`, or the parameter `edit ledger`
    │
    │ Note: run `una explain U2019` for an explanation
────╯

Why: an edit argument is assigned when the call returns, and a plain let binding is never assigned.

Fix: let edit ledger = …. The diagnostic points at the argument, and the fix is at the declaration.

See U2019.

16.7.3 Using a value after take (U2017)

Read the ledger's counts after closing it:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -65,3 +65,4 @@
     lines = lines.append(item: line)
-  close(take ledger, lines: lines)
+  let report = close(take ledger, lines: lines)
+  Report(summary: report.summary, lines: report.lines, closing: ledger.counts)
 
Diagnostic
[U2017] Error: `ledger` was passed by `take`, so it cannot be used afterwards
    ╭─[ src/stockcount.una:67:65 ]
    │
 66 │   let report = close(take ledger, lines: lines)
    │                           ───┬──
    │                              ╰──── passed by `take` here
 67 │   Report(summary: report.summary, lines: report.lines, closing: ledger.counts)
    │                                                                 ───┬──
    │                                                                    ╰──── `ledger` was passed by `take`, so it cannot be used afterwards
    │
    │ Note: run `una explain U2017` for an explanation
────╯

Why: take gave the ledger to close, and the diagnostic shows both places: the first mark is where it was taken, and the second where it is used.

Fix: use what close returns. report.closing already holds the counts.

See U2017.

16.7.4 Wrapping an edit call in attempt (U3013)

Keep the outcome of apply as a Result:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -62,4 +62,4 @@
   for movement in day.movements
-    let line = apply(edit ledger, movement: movement)
-      if error use "rejected: {reason(error)}"
+    let outcome = attempt apply(edit ledger, movement: movement)
+    let line = if outcome.is_ok() then "applied" else "rejected"
     lines = lines.append(item: line)
Diagnostic
[U3013] Error: `apply` is called with `edit` or `take`, so the call must be the whole expression of a statement or `let`; bind its result with `let` first
    ╭─[ src/stockcount.una:63:27 ]
    │
 63 │     let outcome = attempt apply(edit ledger, movement: movement)
    │                           ───────────────────┬──────────────────
    │                                              ╰──────────────────── `apply` is called with `edit` or `take`, so the call must be the whole expression of a statement or `let`; bind its result with `let` first
    │
    │ Note: run `una explain U3013` for an explanation
────╯

Why: a call with an edit or take argument is the whole expression of its statement, so that the binding it changes is updated on that line and nowhere else. The mark covers the whole call, inside the attempt.

Fix: handle the failure with a clause under the call, as close_day does with if error use. To pass the failure on, use if error pass.

See U3013.

16.7.5 old outside ensures (U2012)

Use old(ledger) in the body of apply:

src/stockcount.unadiff
--- a/src/stockcount.una
+++ b/src/stockcount.una
@@ -39,3 +39,3 @@
   ledger.counts = ledger.counts.with(key: movement.sku, value: next)
-  ledger.applied = ledger.applied + 1
+  ledger.applied = old(ledger).applied + 1
   "{movement.sku} {left} -> {next}"
Diagnostic
[U2012] Error: `old` is allowed only in `ensures` clauses
    ╭─[ src/stockcount.una:40:20 ]
    │
 40 │   ledger.applied = old(ledger).applied + 1
    │                    ─────┬─────
    │                         ╰─────── `old` is allowed only in `ensures` clauses
    │
    │ Note: run `una explain U2012` for an explanation
────╯

Why: old names the value on entry for the promise in the header. In the body, the parameter is simply its current value, so the mark points at old(ledger).

Fix: write ledger.applied + 1. To keep the entry value for later in the body, bind it first: let before = ledger.applied.

See U2012.

Next:16.8 Exercises