Home / Learn / 19 Editions
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:
- Constructs. New ways to write code, such as
forloops andlet edit. - Keywords. Words that become part of the language,
edit,take,oldandtogether, and one word reserved for a later edition,weak. - Rules. A few rules that are stricter or different, such as the names a kind may take.
- The prelude. The functions every module has without an import, some of which become generic.
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.
--- 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"--- 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:
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:
[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 explanationThe 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:
$ 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:
- Built-in type names. A kind, a variant or a capability cannot take the name of a built-in type such as
Text,List,MaporU8(U1002). In0.1, a kind with a built-in type's name, evenText, shadows that type in its module. In0.2this is U1002. - Equality on type parameters. A public generic function that compares two values of a type parameter
Tmust write the constraint[T: Eq](U2026, chapter 4);0.1inferred it. - The prelude.
min,max,sumandcontainsbecome generic over constrained types, andsort,setandempty_mapare new. - Recursive kinds, kinds with a field of their own kind (chapter 8), no longer need that field inside a
Listor anOption, but must be able to end: some variant, such as aLit(value: Int)in an expression kind, holds no value of the kind itself. - Test reports. In
0.2, a failed example's report shows labeled values, data marked secret or internal (chapter 17), in full.0.1hides only the labeled fields of a record, writing a level such as<redacted:confidential>in their place; a labeled value that is not a field, such as aconfidential Intresult, is shown in full there too.
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:
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}")lamps in aisle 4Change 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:
[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.