Home / Learn / 19 Editions

Release v0.4.0Edition 0.1, 0.2, 0.3, 0.4

19.2 What edition 0.2 adds

Page 3 of 11

19.2.1 What changes from 0.1 to 0.2

Edition 0.2 changes four things, each of which this section shows. Every rule this section states for 0.2 holds in 0.3 too, since editions are cumulative:

19.2.2 Moving the stock list to 0.2

Move the stock list of section 19.1 to 0.2, and two things that 0.1 rejects become possible: a for loop with a let edit binding, and the interpolation of a whole list.

una.tomldiff
--- a/una.toml
+++ b/una.toml
@@ -1,7 +1,7 @@
 [package]
 name = "shelf"
 version = "0.1.0"
-edition = "0.1"
+edition = "0.2"
 
 [run]
 entry = "shelf"
src/shelf.unadiff
--- a/src/shelf.una
+++ b/src/shelf.una
@@ -5,11 +5,17 @@
   sku: Text
   units: Int
 
---- The units of every line, added up.
-total(lines: List[Line]) -> Int = lines.fold(initial: 0, step: fn(sum, line) -> sum + line.units)
+--- The units of every line, added up by a loop.
+total(lines: List[Line]) -> Int
+=
+  let edit sum = 0
+  for line in lines
+    sum = sum + line.units
+  sum
 
 pub main() -> Unit
   uses @console: Console[print]
 =
   let lines = [Line(sku: "LAMP-01", units: 4), Line(sku: "DESK-02", units: 1)]
   @console.print("{lines.len} lines, {total(lines)} units")
+  @console.print("{lines}")

una run . prints:

Output
2 lines, 5 units
[Line(sku: "LAMP-01", units: 4), Line(sku: "DESK-02", units: 1)]

The code of section 19.1 also checks, unchanged, with only the edition line moved to 0.2: moving forward did not require touching it. The changes to the code are ones the new edition allows. fold is still there, and still right; the loop is simply easier to read for some code.

19.2.3 The constructs 0.2 adds

Edition 0.2 adds, among others:

Construct Where to read about it
for loops, let edit, edit and take parameters, and if without else as a statement Repeating: for, edit, take, if without else
together and together for Concurrency and parallelism
Map, the text functions and ParseError, Json Maps, the text functions, ParseError, JSON
Fixed-width integers such as U8 and I64, with proven overflow Numbers
Constraints on generics such as T: Ord, and sort Constraints, sort
uses of p and fails of p, header clauses for a function that takes another function as its parameter p: the function passed in may then use capabilities and fail, and the caller handles it uses of, fails of, uses in the docs
The interpolation of any value that == compares, except a Hashed, a fingerprint of secret data that only a host capability computes and that a program cannot read Text and conditions
The Http capability HTTP in the standard library

19.2.4 The keywords 0.2 adds

Edition 0.2 makes four words keywords: edit, take, old and together. Each one starts a construct, so in 0.2 none of them can name a binding, a parameter or a field. edit, take and together were reserved in 0.1 already, so no 0.1 package uses them as names. old was not, so a 0.1 package may use it, and must rename it to move, as Why a language needs editions showed.

Edition 0.2 also reserves a word for a later edition: weak. In 0.1 it is an ordinary name, so a 0.1 package may use it, and must rename it to move. This package names a parameter weak, for a week when sales are slow. It checks and runs in 0.1; in 0.2, each of its three uses is U0103, the first:

Diagnostic
[U0103] Error: `weak` is reserved for a later edition (aliasing)
   ╭─[ src/restock.una:4:26 ]
   │
 4 │ weekly_order(units: Int, weak: Bool) -> Int = if weak then units / 2 else units
   │                          ──┬─
   │                            ╰─── `weak` is reserved for a later edition (aliasing)
   │
   │ Help: repair 1: rename it to `the_weak`; edit src/restock.una:4:26..4:30 to "the_weak"
   │
   │ Note: run `una explain U0103` for an explanation

The message says that the word is kept for a later edition, and for what: aliasing. Here the word is meant as a name, so the rename is right, and una fix --dry-run . lists it at every use:

Terminal
$ una fix --dry-run .
fixed src/restock.una:4:26 U0103 rename it to `the_weak`
fixed src/restock.una:4:50 U0103 rename it to `the_weak`
fixed src/restock.una:9:43 U0103 rename it to `the_weak`

una fix . applies the three renames, and the package checks in 0.2. A better name, such as slow_week, works too.

One word goes the other way. with is a keyword in every edition, for with @catalog = … (chapter 12). From edition 0.2 on, with right after a . is a name, the method of a map that adds an entry: prices.with(key: sku, value: 1990).

19.2.5 The rules that changed

A few rules of 0.2 differ from 0.1:

19.2.6 What a move from 0.1 can need

So moving a 0.1 package to 0.2 can still need changes, in five places: a name that became a keyword, such as a binding called old; a name that 0.2 reserves, such as weak, which is U0103; a kind named like a built-in type, which is U1002 in 0.2; a public generic function that compares values of a type parameter without [T: Eq], which is U2026; and a call that relied on a prelude header of 0.1. The checker reports each one at its place, with the code and a repair, so the move is a list of small, local changes rather than a search through the package.

19.2.7 Check yourself: a kind named Map

This package is in edition 0.1. It declares a record named Map, for a floor map of the store, and it checks and runs:

steps/08-floor-map/src/shelf.unacheckrununa
module shelf

--- A floor map of the store: the aisle of a department.
kind Map
  department: Text
  aisle: Int

pub main() -> Unit
  uses @console: Console[print]
=
  let lamps = Map(department: "lamps", aisle: 4)
  @console.print("{lamps.department} in aisle {lamps.aisle}")
Output
lamps in aisle 4

Change only its edition to 0.2. Does it still check?

Show the answer

No. From edition 0.2 on, Map is the built-in type of maps, and a kind cannot take its name:

Diagnostic
[U1002] Error: `Map` is a built-in type of maps from edition 0.2, so a kind cannot take its name
   ╭─[ src/shelf.una:4:6 ]
   │
 4 │ kind Map
   │      ─┬─
   │       ╰─── `Map` is a built-in type of maps from edition 0.2, so a kind cannot take its name
   │
   │ Note: run `una explain U1002` for an explanation
───╯

The mark is on the name in the declaration. Rename the kind, such as to FloorMap, and every use of it, and the package checks in 0.2. The diagnostic has no exact edit, since the new name is a choice.

Next:19.3 Edition 0.3, a preview