---
title: "15.6 Common mistakes"
url: https://una-lang.org/en/learn/concurrency/common-mistakes/
release: v0.4.0
edition: "0.2"
---

# 15.6 Common mistakes

Each mistake is a small change to `day-to-day/src/availability.una`, followed by what `una check .` reports. None of these diagnostics carries an edit that `una fix` can apply: each repair depends on what you meant.

## 15.6.1 Counting in a shared name inside `together for` (U2023)

:::mistake
Count the lines checked in an outer `let edit` name:

```diff include="day-to-day..mistakes/1:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -56,5 +56,8 @@
 =
-  together for line in lines
+  let edit asked = 0
+  let found = together for line in lines
+    asked = asked + 1
     availability(line)
       if error pass
+  found
 
```

```text include="mistakes/1/expected/rendered.txt"
[U2023] Error: `asked` is declared outside this `together` task, which captures its value and cannot change it, so it cannot be assigned here; return the new value from the task instead
    ╭─[ src/availability.una:59:5 ]
    │
 59 │     asked = asked + 1
    │     ──┬──
    │       ╰──── `asked` is declared outside this `together` task, which captures its value and cannot change it, so it cannot be assigned here; return the new value from the task instead
    │
    │ Note: run `una explain U2023` for an explanation
────╯
```

*Why:* each task captures the names around it as they were when the block started, and runs at the same time as the others. An assignment to an outer name would be two clerks writing on one whiteboard: a race, which Una refuses. The marks point at the name being assigned.

*Fix:* return what each task computes, and combine the results after the block. Here the count is `found.len`. See [U2023](https://una-lang.org/en/docs/errors/U2023/).
:::

## 15.6.2 Building the result inside the block (U2024, U1001)

:::mistake
Indent the line that builds the record, so that it stands under `together`:

```diff include="day-to-day..mistakes/2:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -47,3 +47,3 @@
       if error pass
-  Availability(sku: line.sku, wanted: line.quantity, north: north, south: south, east: east)
+    Availability(sku: line.sku, wanted: line.quantity, north: north, south: south, east: east)
 
```

```text include="mistakes/2/expected/rendered.txt#L1-L13"
[U2024] Error: the body of `together` holds only `let` statements, one per branch; call a function from a branch's `let` for more steps
    ╭─[ src/availability.una:41:3 ]
    │
 41 │   together
    │   ────┬───
    │       ╰───── the body of `together` holds only `let` statements, one per branch; call a function from a branch's `let` for more steps
    │
 48 │     Availability(sku: line.sku, wanted: line.quantity, north: north, south: south, east: east)
    │     ─────────────────────────────────────────────┬────────────────────────────────────────────
    │                                                  ╰────────────────────────────────────────────── not a `let`
    │
    │ Note: run `una explain U2024` for an explanation
────╯
```

*Why:* every line under `together` belongs to the block, and the block holds only `let` lines, one per task. The marks point at `together` and at the line that is not a `let`. Three more diagnostics follow, U1001 at `north`, `south` and `east`: the names that the block's tasks bind exist only after the block, so a line inside it cannot read them.

*Fix:* write the line that uses the branches' names after the block, at the indentation of `together`. See [U2024](https://una-lang.org/en/docs/errors/U2024/).
:::

## 15.6.3 Naming a `together` block with `let` (U0101)

:::mistake
Give the block a name, as if it were a value:

```diff include="day-to-day..mistakes/3:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -40,3 +40,3 @@
 =
-  together
+  let counts = together
     let north = @north.on_hand(line.sku)
```

```text include="mistakes/3/expected/rendered.txt"
[U0101] Error: `together` with branches is a statement; write `together for` to build a list
    ╭─[ src/availability.una:41:16 ]
    │
 41 │   let counts = together
    │                ────┬───
    │                    ╰───── `together` with branches is a statement; write `together for` to build a list
    │
    │ Note: run `una explain U0101` for an explanation
────╯
```

*Why:* `together` with branches is a statement. Its results are the names of its branches, `north`, `south` and `east`, so there is no single value for `counts` to hold. The message also names the form that does give a value, `together for`, for a list of results.

*Fix:* remove `let counts =`, and use the names of the branches after the block. See [U0101](https://una-lang.org/en/docs/errors/U0101/).
:::

## 15.6.4 A branch without its clause (U3007)

:::mistake
Leave out the clause of the east branch:

```diff include="day-to-day..mistakes/4:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -46,3 +46,2 @@
     let east = @east.on_hand(line.sku)
-      if error pass
   Availability(sku: line.sku, wanted: line.quantity, north: north, south: south, east: east)
```

```text include="mistakes/4/expected/rendered.txt"
[U3007] Error: `@east.on_hand` can fail with `StockError`; handle the failure with an `if error` clause, or wrap the call in `attempt`
    ╭─[ src/availability.una:46:16 ]
    │
 46 │     let east = @east.on_hand(line.sku)
    │                ───────────┬───────────
    │                           ╰───────────── `@east.on_hand` can fail with `StockError`; handle the failure with an `if error` clause, or wrap the call in `attempt`
    │
    │ Note: run `una explain U3007` for an explanation
────╯
```

*Why:* a branch is a `let` line, and handles its failures as that `let` would outside the block. `@east.on_hand` can fail, so its line needs a clause, as every call that can fail does ([chapter 7](https://una-lang.org/en/learn/failure/handling-a-failure-the-clauses/#s7-3)). The other two branches still have theirs; each branch is checked on its own.

*Fix:* put the clause back under the branch, `if error pass`, or choose another, such as `if error use 0` for a count that may miss a warehouse. See [U3007](https://una-lang.org/en/docs/errors/U3007/).
:::

## 15.6.5 Printing with `together for` (U0101)

:::mistake
Print the lines of the report with `together for`, to print them all at once:

```diff include="day-to-day..mistakes/5:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -98,3 +98,3 @@
     is Ok(found) ->
-      for line in found
+      together for line in found
         @console.print("  {report_line(line)}")
```

```text include="mistakes/5/expected/rendered.txt"
[U0101] Error: a `together for` must be the last line of its block or the value of a `let` or of an assignment
    ╭─[ src/availability.una:99:7 ]
    │
 99 │       together for line in found
    │       ────┬───
    │           ╰───── a `together for` must be the last line of its block or the value of a `let` or of an assignment
    │
    │ Note: run `una explain U0101` for an explanation
────╯
```

*Why:* `together for` builds a list, so it must stand where its value is used: named by a `let`, assigned to a `let edit` name, or as the last line of its block. Here it is neither, since the summary line follows it. Even where it is allowed, printing from tasks would print the lines in an order that timing decides.

*Fix:* keep the plain `for` loop. Printing after the check, in order, is what a report wants. See [U0101](https://una-lang.org/en/docs/errors/U0101/).
:::

## 15.6.6 Writing `await` (U0103)

:::mistake
Wait for the warehouse with `await`, as some other languages do:

```diff include="day-to-day..mistakes/6:src/availability.una" context="1"
--- a/src/availability.una
+++ b/src/availability.una
@@ -41,3 +41,3 @@
   together
-    let north = @north.on_hand(line.sku)
+    let north = await @north.on_hand(line.sku)
       if error pass
```

```text include="mistakes/6/expected/rendered.txt"
[U0103] Error: `await` is not supported; use `together`
    ╭─[ src/availability.una:42:17 ]
    │
 42 │     let north = await @north.on_hand(line.sku)
    │                 ──┬──
    │                   ╰──── `await` is not supported; use `together`
    │
    │ Note: run `una explain U0103` for an explanation
────╯
```

*Why:* in Una, every call already waits for its answer, and work that should wait at the same time goes in a `together` block. `await` and `async` are reserved words that Una does not use, so the check reports them and names the construct to use.

*Fix:* remove `await`. The branch already runs at the same time as its siblings. See [U0103](https://una-lang.org/en/docs/errors/U0103/).
:::
