Home / Learn / 2 Numbers

Release v0.4.0Edition 0.2

2.6 Common mistakes

Page 7 of 9

Each mistake is a small change to day-to-day/src/slip.una, followed by what una check . reports. Read each diagnostic as chapter 1 shows: the message, then the place, then the help.

2.6.1 Printing a number (U2001)

Print the total itself, instead of the line that holds it:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -49,3 +49,3 @@
   @console.print(box_line(boxes(units)))
-  @console.print(total_line(lamp_cents + rug_cents))
+  @console.print(lamp_cents + rug_cents)
   @console.print(closing())
Diagnostic
[U2001] Error: argument `line` of operation `Console.print` must be `Text`, found `Int`

Why: print takes a Text, and lamp_cents + rug_cents is an Int. Una turns no value into another type unless the code says so. The message names the parameter, line, the type it must have and the type it got.

Fix: put the number in a text, "{lamp_cents + rug_cents}", or call total_line, which does. See U2001.

2.6.2 A price with a decimal point (U0101)

Write the price of a lamp as it appears on a shelf tag:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -41,3 +41,3 @@
   let units = lamps + rugs
-  let lamp_cents = line_cents(lamps, cents: 1990)
+  let lamp_cents = line_cents(lamps, cents: 19.90)
   let rug_cents = line_cents(rugs, cents: 4500)
Diagnostic
[U0101] Error: `19.90` is a fractional literal; Una's integer literals are decimal

Why: Una has no type for numbers with a fraction, so 19.90 is not a literal it reads. The marks point at 90, the digits after the point, which no integer literal can have.

Fix: write the price in cents, 1990, as every amount of the slip is. See U0101.

2.6.3 A number with a leading zero (U0007)

Write the order number as a form would, with a leading zero:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -37,3 +37,3 @@
 =
-  let order: U32 = 1042
+  let order: U32 = 01042
   let lamps = 2
Diagnostic
[U0007] Error: malformed integer literal `01042`
    ╭─[ src/slip.una:38:20 ]
    │
 38 │   let order: U32 = 01042
    │                    ──┬──
    │                      ╰──── malformed integer literal `01042`
    │
    │ Help: repair 1: write `1042`; edit src/slip.una:38:20..38:25 to "1042"
    │
    │ Note: run `una explain U0007` for an explanation
────╯
1 of 1 diagnostics have an edit that `una fix` applies (`una fix --dry-run` lists them)

Why: an integer literal has no leading zero, except 0 itself, so 01042 is a malformed literal: it breaks the rules of how a literal is written. The Help line gives the literal it means, and the last line of the output says that una fix . can make that edit.

Fix: write 1042. A number whose zeros matter, such as a postal code, is a Text. See U0007.

2.6.4 An Int where a U32 is expected (U2001)

Leave out the type of order:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -37,3 +37,3 @@
 =
-  let order: U32 = 1042
+  let order = 1042
   let lamps = 2
Diagnostic
[U2001] Error: argument `order` of `title` must be `U32`, found `Int`
    ╭─[ src/slip.una:44:24 ]
    │
 44 │   @console.print(title(order))
    │                        ──┬──
    │                          ╰──── argument `order` of `title` must be `U32`, found `Int`

Why: without a type, let order = 1042 makes order an Int, and title takes a U32. The two are different types, and Una converts neither into the other on its own. The marks point at the argument, not at the let: the name is fine until it reaches a place that wants a U32.

Fix: write the type, let order: U32 = 1042, so that the literal is a U32. See U2001.

2.6.5 A number too large for its type (U2022)

Give the order a number past the range of a U32:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -37,3 +37,3 @@
 =
-  let order: U32 = 1042
+  let order: U32 = 5_000_000_000
   let lamps = 2
Diagnostic
[U2022] Error: the literal `5000000000` does not fit `U32`, which holds `0` to `4294967295`
    ╭─[ src/slip.una:38:20 ]
    │
 38 │   let order: U32 = 5_000_000_000
    │                    ──────┬──────
    │                          ╰──────── the literal `5000000000` does not fit `U32`, which holds `0` to `4294967295`

Why: a U32 holds 0 to 4294967295, and 5000000000 is larger. The check refuses the literal at once, before any proof. The message writes the literal without its _ and gives the range of the type.

Fix: use a number inside the range, or, when order numbers really grow past it, a wider type, U64, in title and in main. See U2022.

2.6.6 One name made twice (U1002)

Copy the line of lamps for the rugs, and forget to change the name:

diff
--- a/src/slip.una
+++ b/src/slip.una
@@ -39,3 +39,3 @@
   let lamps = 2
-  let rugs = 1
+  let lamps = 1
   let units = lamps + rugs
Diagnostic
[U1002] Error: `lamps` is already bound in this scope; a local cannot rebind another
    ╭─[ src/slip.una:40:7 ]
    │
 39 │   let lamps = 2
    │       ──┬──
    │         ╰──── first defined here
 40 │   let lamps = 1
    │       ──┬──
    │         ╰──── `lamps` is already bound in this scope; a local cannot rebind another
    │
    │ Note: run `una explain U1002` for an explanation
────╯
[U1001] Error: cannot find name `rugs` in this module, its imports or the prelude

Why: a body makes each name once, so the second let lamps is refused, with marks on both lines. Three more diagnostics follow, each U1001: the lines that use rugs cannot find it, since no let makes it now. One slip, four reports: fix the first, and the others go with it.

Fix: write let rugs = 1. See U1002.

Next:2.7 Exercises