Each mistake is a small change to day-to-day/src/report.una, except the last, which changes the shipping report of A promotion that cannot overcharge. Each is followed by what una check . reports.
--- a/src/report.una+++ b/src/report.una@@ -137,2 +137,3 @@
let lines = read_all(export)
+ let first = export[0]
let skipped = export.len - lines.len
Diagnostic
[U0101]Error: unexpected `[`, expected end of line
Why: many languages read a position with brackets, so the hand writes them by habit. Una has no index syntax: reading a position can find nothing, so it is a function that gives an Option. The message reads the line as the parser does. export is a complete value, and after it the parser expected the line to end; the [ is where it gave up.
Fix:export.at(index: 0) or export.first(), then when or or_else. See U0101.
Leave out the default when reading the units so far:
diff
--- a/src/report.una+++ b/src/report.una@@ -83,3 +83,3 @@
for line in lines
- let before = units.get(key: line.sku).or_else(default: 0)+ let before = units.get(key: line.sku)
edit units.set(key: line.sku, value: before + line.quantity)
Diagnostic
[U2001]Error: an operand of `+` must be `Int`, found `Option[Int]`
Why:get gives an Option[Int], and + needs an Int. The message names the operator, the type it needs, and the type it found. The checker does not guess what a missing SKU means: 0 is right here, but it would be wrong for a price.
Fix:or_else(default: 0), or a when when the absence needs its own handling. See U2001.
--- a/src/report.una+++ b/src/report.una@@ -98,3 +98,2 @@
is Some(entry) -> "best seller: {entry.key}, {entry.value} units"
- is None -> "no sales"
Diagnostic
[U2007]Error:`when` does not match variant `None` of `Option`│Help: repair 1: add an arm for each missing variant, such as `is None`, or an `else` arm
Why: a when must cover every variant, and None is the day without sales. The message names the variant that has no arm, None, and the kind it belongs to; the help line offers the two repairs, an arm for it or an else.
Fix: add the arm, is None -> "no sales". See U2007.
--- a/src/report.una+++ b/src/report.una@@ -136,3 +136,3 @@
let export = sample_export()
- let lines = read_all(export)+ let lines = read_all(export).sort()
let skipped = export.len - lines.len
Diagnostic
[U2025]Error:`sort` needs its type parameter `T` to satisfy `Ord`, and `Line` does not
Why:sort needs an order, and a record has none: should lines sort by SKU, by category, by price? In the message, T is the element type of the list, here Line, and Ord is the constraint of types with an order (section 4.4).
Fix: sort something that has an order, such as lines.map(each: fn(line) -> line.sku).sort(), or group into a map keyed by what you want to sort by, as exercise 3 does. See U2025.
--- a/src/report.una+++ b/src/report.una@@ -84,3 +84,3 @@
let before = units.get(key: line.sku).or_else(default: 0)
- edit units.set(key: line.sku, value: before + line.quantity)+ units.set(key: line.sku, value: before + line.quantity)
units
Diagnostic
[U2020]Error:`set` updates the map, so the call is written `edit units.set(...)`
Why: a call that reads, such as get, needs no mark, so it is easy to forget that set is different. set changes the map, and every change is marked where it happens. The message names the operation and writes the call as it must be.
Fix:edit units.set(...), as the message says. See U2020.
--- a/src/report.una+++ b/src/report.una@@ -36,3 +36,2 @@
let json = text.parse_json()
- if error use Null
when json
Diagnostic
[U3007]Error:`parse_json` can fail with `JsonError`; handle the failure with an `if error` clause, or wrap the call in `attempt`
Why:parse_json can fail with JsonError, and a call that can fail takes a clause on the line under it. The message names the call, the kind it fails with, and the two ways to handle it: an if error clause, or attempt.
Fix: add a clause, such as if error use Null, as section 7.3 shows. See U3007.
[U0007]Error: unescaped `{` in text, where no interpolation can start; write `\{`│Help: repair 1: write `\{`; edit src/report.una:129:31..129:32 to "\\{"
Why: JSON is full of braces, and in a Una text { starts an interpolation. The message says that the brace cannot start one here, and the help line gives the exact edit, with the line and the columns of the brace.
Fix: write \{ and \}, or let una fix . write them for you. See U0007.
Name the members' rule of the shipping report before passing it:
diff
--- a/src/shipping.una+++ b/src/shipping.una@@ -74,3 +74,4 @@
show(1045, charge: weekend)
- let members = promoted(lamps, promotion: fn(cents) -> cents * 80 / 100)+ let members_rule = fn(cents) -> cents * 80 / 100+ let members = promoted(lamps, promotion: members_rule)
show(1046, charge: members)
Diagnostic
[U2010]Error: cannot infer the type of lambda parameter `cents`; annotate it, as in `cents: Int`
Why: a lambda's parameter types come from the place it is passed to. A let gives it none, so the checker cannot know that cents is an Int. The message names the parameter and the annotation to add.
Fix:fn(cents: Int) -> cents * 80 / 100, or pass the lambda straight to promoted. See U2010.