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

# 6.6 Common mistakes

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

## 6.6.1 A second argument without its label (U2004)

:::mistake
Pass the charge without its label:

```diff include="day-to-day..mistakes/1:src/shipping.una" context="1"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -77,3 +77,3 @@
   let lamp = Order(number: 1042, goods: 3_980, parcel: Parcel(grams: 1_200, zone: Home))
-  show(lamp, charge: lamp.quote)
+  show(lamp, lamp.quote)
   let desk = Order(number: 1043, goods: 15_900, parcel: Parcel(grams: 24_000, zone: Home))
```

```text include="mistakes/1/expected/rendered.txt#L1,L8"
[U2004] Error: argument 2 of `show` needs a label; only the first argument may go without one
    │ Help: repair 1: label it `charge`; edit src/shipping.una:78:14..78:24 to "charge: lamp.quote"
```

*Why:* in many languages arguments are matched by their place, so this habit comes first. In Una only the first argument may go without a label. The message names the argument by its place, 2, and the marks point at it, `lamp.quote`: a call, not the name `charge`, so punning does not apply.

*Fix:* write `charge: lamp.quote`, the label the `Help` line names. The repair is an exact edit, which `una fix .` applies. See [U2004](https://una-lang.org/en/docs/errors/U2004/).
:::

## 6.6.2 Branches of two types (U2001)

:::mistake
Give back the number itself when the order does not ship free:

```diff include="day-to-day..mistakes/2:src/shipping.una" context="0"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -65 +65 @@
-pub report_line(order: Order, charge: Int) -> Text = if charge == 0 then "order {order.number}: ships free" else "order {order.number}: {money(charge)}"
+pub report_line(order: Order, charge: Int) -> Text = if charge == 0 then "order {order.number}: ships free" else charge
```

```text include="mistakes/2/expected/rendered.txt#L1"
[U2001] Error: `report_line` returns `Text`, found `Int`
```

*Why:* both branches of the `if` give the function's result, which is a `Text`, and `charge` is an `Int`. The mistake comes from thinking of the `else` as a fallback that may be anything; it is the other half of one value. The marks point at the branch with the wrong type, not at the callers of `report_line`, which still rely on its header.

*Fix:* a text in both branches, such as `"order {order.number}: {money(charge)}"`. See [U2001](https://una-lang.org/en/docs/errors/U2001/).
:::

## 6.6.3 A result nobody uses (U2011)

:::mistake
Work out a quote on a line of its own, before printing it:

```diff include="day-to-day..mistakes/3:src/shipping.una" context="1"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -77,2 +77,3 @@
   let lamp = Order(number: 1042, goods: 3_980, parcel: Parcel(grams: 1_200, zone: Home))
+  lamp.quote
   show(lamp, charge: lamp.quote)
```

```text include="mistakes/3/expected/rendered.txt#L1-L6"
[U2011] Error: this statement has type `Int`, and only `Unit` values can be discarded; bind the value with `let`
    ╭─[ src/shipping.una:78:3 ]
    │
 78 │   lamp.quote
    │   ─────┬────
    │        ╰────── this statement has type `Int`, and only `Unit` values can be discarded; bind the value with `let`
```

*Why:* `lamp.quote` gives an `Int`, and a statement may only throw away a value of type `Unit`. The quote is worked out, then lost: nothing names it, and the next line works it out again. The message says what to do.

*Fix:* remove the line, or bind the quote, `let charge = lamp.quote`, and pass `charge` on. See [U2011](https://una-lang.org/en/docs/errors/U2011/).
:::

## 6.6.4 A promise the caller needs, left out (U5001)

:::mistake
Remove the postcondition of `quote`:

```diff include="day-to-day..mistakes/4:src/shipping.una" context="1"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -49,3 +49,2 @@
   requires order.parcel.grams > 0
-  ensures result >= 0
 =
```

```text include="mistakes/4/expected/rendered.txt#L1-L10"
[U5001] Error: precondition `chair.quote >= 0` of this call is refuted
    ╭─[ src/shipping.una:83:23 ]
    │
 55 │   requires charge >= 0
    │            ─────┬─────
    │                 ╰─────── requires clause
    │
 83 │   show(chair, charge: weekend_price(chair.quote))
    │                       ─────────────┬────────────
    │                                    ╰────────────── precondition `chair.quote >= 0` of this call is refuted
```

*Why:* the verifier proves the call of `weekend_price` with the header of `quote`, not its body. Without its `ensures`, a quote could be any `Int`, a negative one too, so the precondition of `weekend_price`, `chair.quote >= 0`, cannot be proved. The body never gives back a negative number, which is why the clause looked unneeded. The first mark shows the clause that asks, and the second the call that cannot give it.

*Fix:* put the clause back. A caller can rely only on what a header promises. See [U5001](https://una-lang.org/en/docs/errors/U5001/).
:::

## 6.6.5 A public function that prints without `uses` (U3004)

:::mistake
Make `show` public, in the short form, without its `uses` line:

```diff include="day-to-day..mistakes/5:src/shipping.una" context="1"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -67,6 +67,3 @@
 --- Prints one line of the report.
-show(order: Order, charge: Int) -> Unit
-  uses @console: Console[print]
-=
-  @console.print(report_line(order, charge))
+pub show(order: Order, charge: Int) -> Unit = @console.print(report_line(order, charge))
 
```

```text include="mistakes/5/expected/rendered.txt#L1-L6"
[U3004] Error: `@console` is used here, but this code declares no `uses`, so it is pure
    ╭─[ src/shipping.una:68:47 ]
    │
 68 │ pub show(order: Order, charge: Int) -> Unit = @console.print(report_line(order, charge))
    │                                               ────┬───
    │                                                   ╰───── `@console` is used here, but this code declares no `uses`, so it is pure
```

*Why:* nothing is inferred for a `pub` function. A public header without `uses` promises a pure function, since its callers may be in other modules, and this body prints. The mistake often comes from making a working private function public: it checked before, because its `uses` was inferred, or written.

*Fix:* keep the `uses @console: Console[print]` line in its header, or keep `show` private. See [U3004](https://una-lang.org/en/docs/errors/U3004/).
:::

## 6.6.6 The long form for a one-line function (U7001)

:::mistake
Write `ships_free` with `=` on its own line:

```diff include="day-to-day..mistakes/6:src/shipping.una" context="0"
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -45 +45,3 @@
-pub ships_free(order: Order) -> Bool = order.goods >= 10_000 and order.parcel.zone is Home
+pub ships_free(order: Order) -> Bool
+=
+  order.goods >= 10_000 and order.parcel.zone is Home
```

```text include="mistakes/6/expected/rendered.txt#L1"
[U7001] Error: lines 45 to 47 are not in canonical form
```

*Why:* a function without clauses whose body is one line has one canonical form, the short one. The long form looks tidier to many readers, but Una keeps one layout per function, so that every program reads the same way. The message names the lines, and the `Help` line holds the canonical text.

*Fix:* run `una fmt .`, which rewrites it, or `una fix .`, which applies the `Help` line's edit. See [U7001](https://una-lang.org/en/docs/errors/U7001/).
:::
