Home / Learn / 4 Kinds

Release v0.4.0Edition 0.2

4.7 Common mistakes

Page 8 of 10

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

4.7.1 Leaving out a field (U2009)

Build a parcel without its speed:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -90,3 +90,3 @@
   let lamp = Parcel(order: "A-1001", grams: 1200, zone: National, speed: Standard, delivery: AtLocker("Station 14"))
-  let rug = Parcel(order: "A-1002", grams: 3500, zone: Local, speed: Express, delivery: AtDoor)
+  let rug = Parcel(order: "A-1002", grams: 3500, zone: Local, delivery: AtDoor)
   let gift = Parcel(order: "A-1003", grams: 400, zone: Abroad, speed: Express, delivery: AtStore("Harbour Road"))
Diagnostic
[U2009] Error: `Parcel` is missing field `speed`

Why: a record has a value in every field, and there are no default values. The marks point at Parcel, the constructor, and the message names the field that is missing. A record cannot be half built, so no function ever meets a parcel without a speed.

Fix: give the field, speed: Express. If a parcel may truly have no speed, that is a case of its own: add a variant for it to Speed. See U2009.

4.7.2 A field without its label (U2004)

Leave out the label of a record's first field:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -30,3 +30,3 @@
   when zone
-    is Local -> Charge(item: zone, cents: 300)
+    is Local -> Charge(zone, cents: 300)
     is National -> Charge(item: zone, cents: 450)
Diagnostic
[U2004] Error: `Charge` has 2 fields, so every argument needs its field's label
    ╭─[ src/pricing.una:31:24 ]
    │
 31 │     is Local -> Charge(zone, cents: 300)
    │                        ──┬─
    │                          ╰─── `Charge` has 2 fields, so every argument needs its field's label
    │
    │ Help: repair 1: label it `item`; edit src/pricing.una:31:24..31:28 to "item: zone"
    │
    │ Note: run `una explain U2004` for an explanation

Why: in a call of a function, the first argument may go without its label. A record of two or more fields is different: every field carries its label, the first one too, so that the line says which box each value goes into. The marks point at zone, the value without a label.

Fix: write item: zone, as the Help line says, or let una fix . make that edit. See U2004.

4.7.3 A text where a variant goes (U2001)

Write the zone as a text:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -89,3 +89,3 @@
 =
-  let lamp = Parcel(order: "A-1001", grams: 1200, zone: National, speed: Standard, delivery: AtLocker("Station 14"))
+  let lamp = Parcel(order: "A-1001", grams: 1200, zone: "National", speed: Standard, delivery: AtLocker("Station 14"))
   let rug = Parcel(order: "A-1002", grams: 3500, zone: Local, speed: Express, delivery: AtDoor)
Diagnostic
[U2001] Error: field `zone` of `Parcel` must be `Zone`, found `Text`

Why: "National" is a Text, and the field zone holds a Zone. A variant is a value of its sum, written by its name, without quotes. The message names the field, the type it needs, and the type it found.

Fix: write National, without quotes. A text from outside the program, such as a zone typed by a clerk, is turned into a variant by a function with a when or an if, once, where it comes in. See U2001.

4.7.4 Reading a variant's field without binding it (U1001)

Leave out the binding of the locker, and read it with a dot:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -56,3 +56,3 @@
     is AtDoor -> Charge(item: "to the door", cents: 250)
-    is AtLocker(locker) -> Charge(item: "locker {locker}", cents: 0)
+    is AtLocker -> Charge(item: "locker {delivery.locker}", cents: 0)
     is AtStore(store) -> Charge(item: "store {store}", cents: 0)
Diagnostic
[U1001] Error: `Delivery` has no field `locker`, and no function `locker` in scope applies

Why: delivery is a Delivery, and a Delivery has no field locker: only its variant AtLocker has one. The arm is AtLocker matches the variant, but a dot on delivery still reads the sum, not the variant. The marks point at locker.

Fix: bind the field in the pattern, is AtLocker(locker), and use the name locker in the arm. See U1001.

4.7.5 A when that misses a variant (U2007)

Leave out the arm for Abroad:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -32,3 +32,2 @@
     is National -> Charge(item: zone, cents: 450)
-    is Abroad -> Charge(item: zone, cents: 1200)
 
Diagnostic
[U2007] Error: `when` does not match variant `Abroad` of `Zone`
    ╭─[ src/pricing.una:30:3 ]
    │
 30 │   when zone
    │   ──┬─
    │     ╰─── `when` does not match variant `Abroad` of `Zone`
    │
    │ Help: repair 1: add an arm for each missing variant, such as `is Abroad`, or an `else` arm
    │
    │ Note: run `una explain U2007` for an explanation

Why: a when over a sum matches every variant, so that no value falls through it with no answer. The marks point at the when, and the message names the variant it does not match. The same message comes when a new variant is added to the sum and a when has no arm for it yet.

Fix: add the arm, is Abroad -> Charge(item: zone, cents: 1200). Add an else only when the other variants truly share one answer. See U2007.

4.7.6 A public generic function without its constraint (U2026)

Make charge_line public, and leave out its constraint:

diff
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -72,3 +72,3 @@
 --- One line of the price list. Interpolating `charge.item` needs `Eq`.
-charge_line[T: Eq](charge: Charge[T]) -> Text = "  {charge.item}: {charge.cents} cents"
+pub charge_line[T](charge: Charge[T]) -> Text = "  {charge.item}: {charge.cents} cents"
 
Diagnostic
[U2026] Error: interpolation on values of the type parameter `T` needs the constraint `Eq`; write `[T: Eq]`
    ╭─[ src/pricing.una:73:53 ]
    │
 73 │ pub charge_line[T](charge: Charge[T]) -> Text = "  {charge.item}: {charge.cents} cents"
    │                                                     ─────┬─────
    │                                                          ╰─────── interpolation on values of the type parameter `T` needs the constraint `Eq`; write `[T: Eq]`
    │
    │ Help: repair 1: constrain `T` by `Eq`; edit src/pricing.una:73:17..73:18 to "T: Eq"
    │
    │ Note: run `una explain U2026` for an explanation

Why: interpolation writes charge.item, a value of T, into a text, and that needs Eq. A function without pub may leave the constraint out, and the checker infers it; a public one writes it, so that callers in other modules can read what it needs from its header. The marks point at the interpolation.

Fix: write [T: Eq], as the Help line says, or let una fix . make that edit. See U2026.

Next:4.8 Exercises