Home / Learn / 11 Packages and modules

Release v0.4.0Edition 0.2

11.6 Common mistakes

Page 7 of 9

Each mistake below is a small change to the day-to-day package, followed by what una check . reports.

11.6.1 A module header that does not match its path (U1008)

Add src/store/shipping.una, and name the module after the file alone:

mistakes/1/src/store/shipping.unauna
module shipping

--- The shipping fee in cents: 4.90, and nothing from 50.00 on.
pub fee(subtotal: Int) -> Int = if subtotal >= 5000 then 0 else 490
Diagnostic
[U1008] Error: module `shipping` is in `src/store/shipping.una`, which must hold module `store.shipping`
   ╭─[ src/store/shipping.una:1:8 ]
   │
 1 │ module shipping
   │        ────┬───
   │            ╰───── module `shipping` is in `src/store/shipping.una`, which must hold module `store.shipping`
   │
   │ Help: repair 1: declare `module store.shipping`, or move the file to match `shipping`; edit src/store/shipping.una:1:8..1:16 to "store.shipping"
   │
   │ Note: run `una explain U1008` for an explanation
───╯
1 of 1 diagnostics have an edit that `una fix` applies (`una fix --dry-run` lists them)

Why: a module's name is its path under src/, so that an import says where to find the file. The diagnostic quotes both: the name the line declares, and the name its path requires.

Fix: write module store.shipping. The repair carries that edit, and una fix . applies it. See U1008.

11.6.2 Using a name you did not import (U1001)

Drop member_price from the invoice's import, and keep the call:

src/store/invoice.unadiff
--- a/src/store/invoice.una
+++ b/src/store/invoice.una
@@ -4,3 +4,3 @@
 import store.money (format)
-import store.pricing (line_cents, member_price)
+import store.pricing (line_cents)
 
Diagnostic
[U1001] Error: cannot find name `member_price` in this module, its imports or the prelude
    ╭─[ src/store/invoice.una:26:16 ]
    │
 26 │   let member = member_price(product.cents)
    │                ──────┬─────
    │                      ╰─────── cannot find name `member_price` in this module, its imports or the prelude
    │
    │ Note: run `una explain U1001` for an explanation
────╯

Why: a module sees its own declarations, its imports and the prelude. Being pub in another module is not enough. The mark points at the call, not at the import, since the import line itself is fine.

Fix: add the name to the import of store.pricing. See U1001.

11.6.3 An import that clashes with a declaration (U1002)

Declare a private format in the invoice, next to the imported one:

src/store/invoice.unadiff
--- a/src/store/invoice.una
+++ b/src/store/invoice.una
@@ -21,2 +21,5 @@
 
+--- A quantity as the invoice writes it.
+format(quantity: Int) -> Text = "{quantity} x"
+
 --- One line of the invoice, as text, with the members' price of one unit.
Diagnostic
[U1002] Error: `format` is already defined in this module
    ╭─[ src/store/invoice.una:4:21 ]
    │
  4 │ import store.money (format)
    │                     ───┬──
    │                        ╰──── `format` is already defined in this module
    │
 23 │ format(quantity: Int) -> Text = "{quantity} x"
    │ ───┬──
    │    ╰──── first defined here
    │
    │ Note: run `una explain U1002` for an explanation
────╯

Why: an explicit import and a declaration of the module share one set of names, so one name cannot mean two things. The first mark is on the imported name, and the second, "first defined here", on the new declaration.

Fix: rename the new function, for example quantity_text. See U1002.

11.6.4 An import cycle (U1003)

Add a helper for members' prices to store.money, which then imports store.pricing, which imports store.money:

src/store/money.unadiff
--- a/src/store/money.una
+++ b/src/store/money.una
@@ -1,2 +1,4 @@
 module store.money
+
+import store.pricing (member_price)
 
@@ -7,2 +9,5 @@
 pub format(cents: Int) -> Text = if cents < 0 then "-{digits(0 - cents)}" else digits(cents)
+
+--- What a member pays, as a person reads it.
+pub format_member(cents: Cents) -> Text = format(member_price(cents))
 
Diagnostic
[U1003] Error: import cycle between modules `store.money` and `store.pricing`
   ╭─[ src/store/money.una:3:8 ]
   │
 3 │ import store.pricing (member_price)
   │        ──────┬──────
   │              ╰──────── import cycle between modules `store.money` and `store.pricing`
   │
   │ Note: run `una explain U1003` for an explanation
───╯

Why: modules form a graph with no cycle, so each can be read, and checked, after the modules it uses. The diagnostic names the two modules of the cycle, and points at the import in store.money that starts it.

Fix: put the helper where both of its parts are visible, such as store.pricing or store.invoice. See U1003.

11.6.5 Imports out of order (U7001)

Put store.money before store.catalog:

src/store/invoice.unadiff
--- a/src/store/invoice.una
+++ b/src/store/invoice.una
@@ -2,4 +2,4 @@
 
+import store.money (format)
 import store.catalog (Product, find)
-import store.money (format)
 import store.pricing (line_cents, member_price)
Diagnostic
[U7001] Error: line 3 is not in canonical form
   ╭─[ src/store/invoice.una:3:1 ]
   │
 3 │ import store.money (format)
   │ ─────────────┬─────────────
   │              ╰─────────────── line 3 is not in canonical form
   │
   │ Help: repair 1: rewrite in canonical form, as `una fmt` does; edit src/store/invoice.una:3:1..4:1 to ""
   │
   │ Note: run `una explain U7001` for an explanation
───╯
[U7001] Error: the canonical form adds lines before line 5
   ╭─[ src/store/invoice.una:5:1 ]
   │
 5 │ import store.pricing (line_cents, member_price)
   │ │
   │ ╰─ the canonical form adds lines before line 5
   │
   │ Help: repair 1: rewrite in canonical form, as `una fmt` does; edit src/store/invoice.una:5:1..5:1 to "import store.money (format)\n"
   │
   │ Note: run `una explain U7001` for an explanation
───╯
2 of 2 diagnostics have an edit that `una fix` applies (`una fix --dry-run` lists them)

Why: canonical form sorts imports by module path, and the names inside each import, so two people who add imports never produce two orders. U7001 reports each region that differs from the canonical form: here line 3 must go, and the same import must come back before line 5. Each repair carries its edit.

Fix: una fmt . sorts them, and so does una fix .. See U7001.

Next:11.7 Exercises