Home / Learn / 3 Text and conditions

Release v0.4.0Edition 0.2

3.7 Common mistakes

Page 8 of 10

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

3.7.1 Joining texts with + (U2001)

Build the start of the label with +, as some languages allow:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -48,3 +48,3 @@
   let sku = clean_sku(raw)
-  let line = "{sku} | {fit_name(name)} | {price_text(cents, percent: percent, grams: grams)}"
+  let line = sku + " | " + fit_name(name)
   let noted = if units < 5 then "{line} | {stock_note(units)}" else line
Diagnostic
[U2001] Error: an operand of `+` must be `Int`, found `Text`
    ╭─[ src/labels.una:49:14 ]
    │
 49 │   let line = sku + " | " + fit_name(name)
    │              ─┬─
    │               ╰─── an operand of `+` must be `Int`, found `Text`
    │
    │ Note: run `una explain U2001` for an explanation
────╯

Why: + adds numbers, and a text is not a number; Una turns neither into the other. The check reports each text that + is given, three times here, and a fourth diagnostic follows from them: + gives an Int, so line is an Int, and the if on the next line cannot choose between a text and it. One mistake, four diagnostics, and one fix removes them all.

Fix: join texts with interpolation, "{sku} | {fit_name(name)} | …". See U2001.

3.7.2 A text inside an interpolation (U0006)

Print a label through an interpolation, with its arguments written in place:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -59,3 +59,3 @@
   @console.print(shelf_label("coffee-05", name: "Coffee beans, dark roast", cents: 1250, percent: 0, grams: 500, units: 30))
-  @console.print(shelf_label("DESK-02", name: "Oak writing desk", cents: 15900, percent: 0, grams: 0, units: 0))
+  @console.print("{shelf_label("DESK-02", name: "Oak writing desk", cents: 15900, percent: 0, grams: 0, units: 0)}")
   @console.print(shelf_label("mug 4", name: "Tea mug, blue", cents: 650, percent: 0, grams: 0, units: 8))
Diagnostic
[U0006] Error: text literal inside interpolation; bind it with `let` and interpolate the name
    ╭─[ src/labels.una:60:32 ]
    │
 60 │   @console.print("{shelf_label("DESK-02", name: "Oak writing desk", cents: 15900, percent: 0, grams: 0, units: 0)}")
    │                                ────┬────
    │                                    ╰────── text literal inside interpolation; bind it with `let` and interpolate the name
    │
    │ Note: run `una explain U0006` for an explanation
────╯

Why: the braces of an interpolation cannot hold a text literal, since its quotes would end the outer text. The check reports each of the two literals, "DESK-02" and "Oak writing desk", and the message says how to repair it.

Fix: the braces add nothing here, so print the label itself, @console.print(shelf_label("DESK-02", …)). Where a text must go into braces, name it with let first and put the name there. See U0006.

3.7.3 A number as a condition (U2001)

Test the discount itself, as if a number could be true:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -38,3 +38,3 @@
   let weighed = "{regular} ({money(per_kilo(cents, grams: grams))} per kg)"
-  if on_sale(percent) then sale else if grams > 0 then weighed else regular
+  if percent then sale else if grams > 0 then weighed else regular
 
Diagnostic
[U2001] Error: the condition of `if` must be `Bool`, found `Int`
    ╭─[ src/labels.una:39:6 ]
    │
 39 │   if percent then sale else if grams > 0 then weighed else regular
    │      ───┬───
    │         ╰───── the condition of `if` must be `Bool`, found `Int`

Why: the condition of an if must be a Bool. An Int is never true or false in Una, not even 0. The message names the place, the condition of if, the type it needs, and the type it found.

Fix: say the rule: if on_sale(percent), or if percent > 0 for any discount at all. See U2001.

3.7.4 A chained comparison (U0101)

Write the store's rule for a discount as a chain, as in school:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -22,3 +22,3 @@
 --- Whether a discount is one the store runs: from 1 to 90 percent off.
-pub on_sale(percent: Int) -> Bool = percent >= 1 and percent <= 90
+pub on_sale(percent: Int) -> Bool = 1 <= percent <= 90
 
Diagnostic
[U0101] Error: comparisons cannot be chained; join them with `and`, as in `a < b and b < c`
    ╭─[ src/labels.una:23:50 ]
    │
 23 │ pub on_sale(percent: Int) -> Bool = 1 <= percent <= 90
    │                                                  ─┬
    │                                                   ╰── comparisons cannot be chained; join them with `and`, as in `a < b and b < c`

Why: comparisons in Una do not chain. 1 <= percent is a Bool, and comparing a Bool with 90 has no meaning, so the language refuses the form outright, and the message says what to write instead.

Fix: two comparisons joined with and: percent >= 1 and percent <= 90. See U0101.

3.7.5 Cutting a text without a guard (U5001)

Cut every name to 24 characters, without the if:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -16,3 +16,3 @@
 --- A product name cut to the 24 characters a label has room for.
-pub fit_name(name: Text) -> Text = if name.len > 24 then name.slice(start: 0, end: 24) else name
+pub fit_name(name: Text) -> Text = name.slice(start: 0, end: 24)
 
Diagnostic
[U5001] Error: precondition `0 <= 0 and 0 <= 24 and 24 <= name.len` of this call is refuted
    ╭─[ src/labels.una:17:36 ]
    │
 17 │ pub fit_name(name: Text) -> Text = name.slice(start: 0, end: 24)
    │                                    ──────────────┬──────────────
    │                                                  ╰──────────────── precondition `0 <= 0 and 0 <= 24 and 24 <= name.len` of this call is refuted
    │ Help 2: repair 2: add a guard (`if` or a `when` arm) that handles the values where `0 <= 0 and 0 <= 24 and 24 <= name.len` does not hold
    │ Note 2: counterexample: name = Text#0, name.len = 0

Why: slice requires 24 <= name.len, and nothing says that a name has 24 characters. The counterexample is an empty name, of length 0, from which no 24 characters can be cut. The checker refuses the program rather than let it stop on a short name.

Fix: put the if back, so that only a name longer than 24 characters is cut, as the second repair of the diagnostic suggests. See U5001.

3.7.6 An if without else (U0101)

Leave out the else of the stock note, as if a shelf with units needed no note:

diff
--- a/src/labels.una
+++ b/src/labels.una
@@ -41,3 +41,3 @@
 --- A note on the stock left, for a shelf with fewer than five units.
-pub stock_note(units: Int) -> Text = if units <= 0 then "sold out" else "only {units} left"
+pub stock_note(units: Int) -> Text = if units <= 0 then "sold out"
 
Diagnostic
[U0101] Error: unexpected end of line, expected `else`
    ╭─[ src/labels.una:42:67 ]
    │
 42 │ pub stock_note(units: Int) -> Text = if units <= 0 then "sold out"
    │                                                                   ┬
    │                                                                   ╰── unexpected end of line, expected `else`

Why: an if on one line gives a value, so it needs a value for each answer of its condition. Without else, stock_note(2) would have no text to give back. The marks point at the end of the line, where else was expected.

Fix: give the if its else and the value for false, here else "only {units} left". See U0101.

Next:3.8 Exercises