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

# 8.7 Common mistakes

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

## 8.7.1 A `when` that misses a variant (U2007)

:::mistake
Remove the `Returned` arm from `describe`:

```diff include="day-to-day..mistakes/1:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -86,3 +86,2 @@
     is Delivered(day) -> "delivered on day {day}"
-    is Returned(reason, day) -> "returned on day {day}, {reason_text(reason)}"
 
```

```text include="mistakes/1/expected/rendered.txt#L1-L8"
[U2007] Error: `when` does not match variant `Returned` of `Status`
    ╭─[ src/tracking.una:83:3 ]
    │
 83 │   when status
    │   ──┬─
    │     ╰─── `when` does not match variant `Returned` of `Status`
    │
    │ Help: repair 1: add an arm for each missing variant, such as `is Returned`, or an `else` arm
```

*Why:* a `when` over a sum covers every variant. The marks are on the `when` itself, since the problem is an arm that is not there, and the message names the missing variant and its kind. You get the same error after adding a variant to `Status`, at every `when` that needs an arm for it.

*Fix:* add the arm. Add `else` only when every remaining variant really gets the same answer. See [U2007](https://una-lang.org/en/docs/errors/U2007/).
:::

## 8.7.2 Ranges that leave a gap (U5001)

:::mistake
Replace the `else` of `weight_cents` with a last band:

```diff include="day-to-day..mistakes/2:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -49,3 +49,3 @@
     in 501..2000 -> 150
-    else -> 400
+    in 2001..30000 -> 400
 
```

```text include="mistakes/2/expected/rendered.txt#L1,L8,L12"
[U5001] Error: exhaustiveness of this `when`, `grams in 0..500 or grams in 501..2000 or grams in 2001..30000`, is refuted
    │ Help: repair 1: add an `else` arm, or arms for the values the others miss
    │ Note 2: counterexample: grams = -1
```

*Why:* without an `else`, the verifier must prove that the ranges cover every `Int`, and the first line quotes what it tried to prove. `Note 2` is the counterexample: no arm matches `grams = -1`, nor anything above 30,000. An `Int` can be any whole number, negative ones included.

*Fix:* end with an `else` arm, as the help says. To rule out negative weights for good, give the field a refined kind, as [section 5.3](https://una-lang.org/en/learn/contracts/refined-kinds-and-predicates/#s5-3) shows. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 8.7.3 An `else` before the other arms (U2008)

:::mistake
Put the `else` of `refund` first:

```diff include="day-to-day..mistakes/3:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -107,2 +107,3 @@
   when status
+    else -> 0
     is Returned(reason) ->
@@ -112,3 +113,2 @@
         paid
-    else -> 0
 
```

```text include="mistakes/3/expected/rendered.txt#L1-L9"
[U2008] Error: this arm follows the `else` arm, so no value reaches it; `else` must be the last arm
     ╭─[ src/tracking.una:109:5 ]
     │
 108 │     else -> 0
     │     ──┬─
     │       ╰─── the `else` arm
 109 │     is Returned(reason) ->
     │     ─────────┬─────────
     │              ╰─────────── this arm follows the `else` arm, so no value reaches it; `else` must be the last arm
```

*Why:* the arms are tried in order, and `else` matches every value, so no status would ever reach the `Returned` arm, and every refund would be 0. The first mark shows the `else`, and the second the arm it hides. Una refuses the `when`, since an arm that can never run is always a mistake.

*Fix:* move `else` to the end, where it takes only the values the other arms leave. See [U2008](https://una-lang.org/en/docs/errors/U2008/).
:::

## 8.7.4 A guard without its `else` (U0101)

:::mistake
Leave out the `else` of the guard in `refund`:

```diff include="day-to-day..mistakes/4:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -109,3 +109,3 @@
       if reason == Refused
-        if paid > 600 then paid - 600 else 0
+        if paid > 600 then paid - 600
       else
```

```text include="mistakes/4/expected/rendered.txt#L1-L8"
[U0101] Error: unexpected end of line, expected `else`
     ╭─[ src/tracking.una:110:38 ]
     │
 110 │         if paid > 600 then paid - 600
     │                                      ┬
     │                                      ╰── unexpected end of line, expected `else`
     │
     │ Help: repair 1: an `if` with `then` needs `else`; to fail early, make the rest of the body its `else` branch, as in `if x < 0 then fail Broke else x`, or use the block form, whose `else` block holds the rest
```

*Why:* the one-line `if` gives a value, the refund, so it needs one for both answers of its condition. Without `else`, a refused parcel of 600 cents or less would have no refund at all. The marks point at the end of the line, where `else` was expected.

*Fix:* add `else 0`, the value when the condition is `false`. The `if` without `else` of [section 8.1](https://una-lang.org/en/learn/choices/every-form-of-if/#s8-1) is a statement, and never gives a value. See [U0101](https://una-lang.org/en/docs/errors/U0101/).
:::

## 8.7.5 A guard that does not rule out the bad value (U5001)

:::mistake
Guard the refund with `paid > 0` instead of `paid > 600`:

```diff include="day-to-day..mistakes/5:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -109,3 +109,3 @@
       if reason == Refused
-        if paid > 600 then paid - 600 else 0
+        if paid > 0 then paid - 600 else 0
       else
```

```text include="mistakes/5/expected/rendered.txt#L1-L10,L18"
[U5001] Error: refinement `result >= 0` is refuted
     ╭─[ src/tracking.una:110:26 ]
     │
  13 │ pub kind Cents = Int where it >= 0
     │                            ───┬───
     │                               ╰───── where clause
     │
 110 │         if paid > 0 then paid - 600 else 0
     │                          ─────┬────
     │                               ╰────── refinement `result >= 0` is refuted
     │ Note 2: counterexample: paid = 1, result = -599, status = Returned, status.reason = Refused
```

*Why:* `refund` promises a `Cents`, a value of at least 0. In the `then` branch, the verifier knows only `paid > 0`, which allows `paid = 1`, and then the refund is -599. The marks point at `paid - 600`, the value that breaks the refinement, and at the `where` clause of `Cents` that it breaks. The counterexample names the whole path: the status, its reason, the amount and the result.

*Fix:* a condition that implies the result is at least 0: `paid > 600`, or `paid >= 600`. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 8.7.6 Comparing with a variant that has fields (U2009)

:::mistake
Test the status with `==` in `apply`:

```diff include="day-to-day..mistakes/6:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -57,3 +57,3 @@
     is PickedUp(carrier, tracking) -> if status is Packed then InTransit(carrier, tracking) else status
-    is DeliveredOn(day) -> if status is InTransit then Delivered(day) else status
+    is DeliveredOn(day) -> if status == InTransit then Delivered(day) else status
     is CameBack(reason, day) -> if status is Delivered then Returned(reason, day) else status
```

```text include="mistakes/6/expected/rendered.txt#L1,L8"
[U2009] Error: variant `InTransit` needs its fields: `carrier`, `tracking`
    │ Help: repair 1: write `status is InTransit`; edit src/tracking.una:58:31..58:50 to "status is InTransit"
```

*Why:* `InTransit` on its own is not a value: it needs a carrier and a tracking number. `==` compares two whole values, so it has nothing to compare `status` with. This one comes from other languages, where comparing a status with a name is the usual test.

*Fix:* `status is InTransit`, which tests the variant only. The help line holds the exact edit, and `una fix .` applies it. See [U2009](https://una-lang.org/en/docs/errors/U2009/).
:::

## 8.7.7 A recursive kind with no variant that ends (U2002)

:::mistake
Remove `Start` from `History`, so that every history is one event after another:

```diff include="day-to-day..mistakes/7:src/tracking.una" context="1" title="src/tracking.una"
--- a/src/tracking.una
+++ b/src/tracking.una
@@ -27,3 +27,3 @@
 --- Every event of a shipment, the latest first. `Start` is the history before any event.
-pub kind History = Start | After(event: Event, earlier: History)
+pub kind History = | After(event: Event, earlier: History)
 
```

```text include="mistakes/7/expected/rendered.txt#L1-L9"
[U2002] Error: kind `History` has no finite value: every variant contains a kind without one; add a variant without it, as `End`
    ╭─[ src/tracking.una:28:10 ]
    │
 28 │ pub kind History = | After(event: Event, earlier: History)
    │          ───┬───
    │             ╰───── kind `History` has no finite value: every variant contains a kind without one; add a variant without it, as `End`
    │
    │ Note: run `una explain U2002` for an explanation
────╯
```

*Why:* every `After` needs an earlier history, which would be another `After`, without end, so no `History` can be built. U2002 is reported at the kind. Each use of `Start` in the program is then reported too, as U1001, since the variant no longer exists.

*Fix:* keep a variant that does not contain the kind, here `Start`, as the bottom of every history. See [U2002](https://una-lang.org/en/docs/errors/U2002/).
:::
