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

# 10.8 Common mistakes

Each mistake is a small change to `day-to-day/src/report.una`, except the last, which changes the shipping report of [A promotion that cannot overcharge](https://una-lang.org/en/learn/collections/functions-as-values-and-lambdas/#a-promotion-that-cannot-overcharge). Each is followed by what `una check .` reports.

## 10.8.1 Indexing with brackets (U0101)

:::mistake
Read the first line of the export as `export[0]`:

```diff include="day-to-day..mistakes/1:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -137,2 +137,3 @@
   let lines = read_all(export)
+  let first = export[0]
   let skipped = export.len - lines.len
```

```text include="mistakes/1/expected/rendered.txt#L1"
[U0101] Error: unexpected `[`, expected end of line
```

*Why:* many languages read a position with brackets, so the hand writes them by habit. Una has no index syntax: reading a position can find nothing, so it is a function that gives an `Option`. The message reads the line as the parser does. `export` is a complete value, and after it the parser expected the line to end; the `[` is where it gave up.

*Fix:* `export.at(index: 0)` or `export.first()`, then `when` or `or_else`. See [U0101](https://una-lang.org/en/docs/errors/U0101/).
:::

## 10.8.2 Using an `Option` as its value (U2001)

:::mistake
Leave out the default when reading the units so far:

```diff include="day-to-day..mistakes/2:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -83,3 +83,3 @@
   for line in lines
-    let before = units.get(key: line.sku).or_else(default: 0)
+    let before = units.get(key: line.sku)
     edit units.set(key: line.sku, value: before + line.quantity)
```

```text include="mistakes/2/expected/rendered.txt#L1"
[U2001] Error: an operand of `+` must be `Int`, found `Option[Int]`
```

*Why:* `get` gives an `Option[Int]`, and `+` needs an `Int`. The message names the operator, the type it needs, and the type it found. The checker does not guess what a missing SKU means: 0 is right here, but it would be wrong for a price.

*Fix:* `or_else(default: 0)`, or a `when` when the absence needs its own handling. See [U2001](https://una-lang.org/en/docs/errors/U2001/).
:::

## 10.8.3 A `when` that forgets `None` (U2007)

:::mistake
Remove the `is None` arm of `headline`:

```diff include="day-to-day..mistakes/3:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -98,3 +98,2 @@
     is Some(entry) -> "best seller: {entry.key}, {entry.value} units"
-    is None -> "no sales"
 
```

```text include="mistakes/3/expected/rendered.txt#L1,L8"
[U2007] Error: `when` does not match variant `None` of `Option`
    │ Help: repair 1: add an arm for each missing variant, such as `is None`, or an `else` arm
```

*Why:* a `when` must cover every variant, and `None` is the day without sales. The message names the variant that has no arm, `None`, and the kind it belongs to; the help line offers the two repairs, an arm for it or an `else`.

*Fix:* add the arm, `is None -> "no sales"`. See [U2007](https://una-lang.org/en/docs/errors/U2007/).
:::

## 10.8.4 Sorting records (U2025)

:::mistake
Sort the lines as they are read:

```diff include="day-to-day..mistakes/4:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -136,3 +136,3 @@
   let export = sample_export()
-  let lines = read_all(export)
+  let lines = read_all(export).sort()
   let skipped = export.len - lines.len
```

```text include="mistakes/4/expected/rendered.txt#L1"
[U2025] Error: `sort` needs its type parameter `T` to satisfy `Ord`, and `Line` does not
```

*Why:* `sort` needs an order, and a record has none: should lines sort by SKU, by category, by price? In the message, `T` is the element type of the list, here `Line`, and `Ord` is the constraint of types with an order ([section 4.4](https://una-lang.org/en/learn/kinds/generic-kinds-and-constraints/#s4-4)).

*Fix:* sort something that has an order, such as `lines.map(each: fn(line) -> line.sku).sort()`, or group into a map keyed by what you want to sort by, as [exercise 3](https://una-lang.org/en/learn/collections/exercises/#exercise-3-categories-by-revenue) does. See [U2025](https://una-lang.org/en/docs/errors/U2025/).
:::

## 10.8.5 Calling `set` without `edit` (U2020)

:::mistake
Write the update in `units_by_sku` as a plain call:

```diff include="day-to-day..mistakes/5:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -84,3 +84,3 @@
     let before = units.get(key: line.sku).or_else(default: 0)
-    edit units.set(key: line.sku, value: before + line.quantity)
+    units.set(key: line.sku, value: before + line.quantity)
   units
```

```text include="mistakes/5/expected/rendered.txt#L1"
[U2020] Error: `set` updates the map, so the call is written `edit units.set(...)`
```

*Why:* a call that reads, such as `get`, needs no mark, so it is easy to forget that `set` is different. `set` changes the map, and every change is marked where it happens. The message names the operation and writes the call as it must be.

*Fix:* `edit units.set(...)`, as the message says. See [U2020](https://una-lang.org/en/docs/errors/U2020/).
:::

## 10.8.6 Parsing JSON without a clause (U3007)

:::mistake
Remove `if error use Null` from `read_line`:

```diff include="day-to-day..mistakes/6:src/report.una" context="1"
--- a/src/report.una
+++ b/src/report.una
@@ -36,3 +36,2 @@
   let json = text.parse_json()
-    if error use Null
   when json
```

```text include="mistakes/6/expected/rendered.txt#L1"
[U3007] Error: `parse_json` can fail with `JsonError`; handle the failure with an `if error` clause, or wrap the call in `attempt`
```

*Why:* `parse_json` can fail with `JsonError`, and a call that can fail takes a clause on the line under it. The message names the call, the kind it fails with, and the two ways to handle it: an `if error` clause, or `attempt`.

*Fix:* add a clause, such as `if error use Null`, as [section 7.3](https://una-lang.org/en/learn/failure/handling-a-failure-the-clauses/#s7-3) shows. See [U3007](https://una-lang.org/en/docs/errors/U3007/).
:::

## 10.8.7 A brace in JSON text (U0007)

:::mistake
Write a JSON object in a text literal without escaping its braces:

```diff include="day-to-day..mistakes/7:src/report.una" context="0"
--- a/src/report.una
+++ b/src/report.una
@@ -129 +129 @@
-  lines = lines.append(item: "\{\"sku\": \"VASE-03\", \"quantity\": 2, \"cents\": 2400\}")
+  lines = lines.append(item: "{\"sku\": \"VASE-03\", \"quantity\": 2, \"cents\": 2400}")
```

```text include="mistakes/7/expected/rendered.txt#L1,L8"
[U0007] Error: unescaped `{` in text, where no interpolation can start; write `\{`
     │ Help: repair 1: write `\{`; edit src/report.una:129:31..129:32 to "\\{"
```

*Why:* JSON is full of braces, and in a Una text `{` starts an interpolation. The message says that the brace cannot start one here, and the help line gives the exact edit, with the line and the columns of the brace.

*Fix:* write `\{` and `\}`, or let `una fix .` write them for you. See [U0007](https://una-lang.org/en/docs/errors/U0007/).
:::

## 10.8.8 A lambda named with `let` (U2010)

:::mistake
Name the members' rule of the shipping report before passing it:

```diff include="steps/12-shipping-quotes..mistakes/8:src/shipping.una" context="1"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -74,3 +74,4 @@
   show(1045, charge: weekend)
-  let members = promoted(lamps, promotion: fn(cents) -> cents * 80 / 100)
+  let members_rule = fn(cents) -> cents * 80 / 100
+  let members = promoted(lamps, promotion: members_rule)
   show(1046, charge: members)
```

```text include="mistakes/8/expected/rendered.txt#L1"
[U2010] Error: cannot infer the type of lambda parameter `cents`; annotate it, as in `cents: Int`
```

*Why:* a lambda's parameter types come from the place it is passed to. A `let` gives it none, so the checker cannot know that `cents` is an `Int`. The message names the parameter and the annotation to add.

*Fix:* `fn(cents: Int) -> cents * 80 / 100`, or pass the lambda straight to `promoted`. See [U2010](https://una-lang.org/en/docs/errors/U2010/).
:::
