Lesson 5 — Kinds and nothing
Step 1: saying the kind out loud
Every value is of one kind: number, text, boolean, list, object, or bytes — and there is one more, nothing, which gets Step 2. A kind can be written, as a mark: a kind word after a colon, on a let, on a function's parameters, or after -> on a function to say what it gives. A mark is a promise the language checks — at every boundary the value must be of the marked kind, or the program is refused in plain words:
The book says it answers:
58 Shalom
Marks are optional everywhere; an unmarked program is checked just as hard in every way that doesn't need a mark. A mark adds the kind promise on top.
Your turn. Keep a text mark on a shelf holding your name, and mark a function triple so it promises a number in and a number out. Say triple(4).
The worked answer — try yours first, then peek
The book says it answers:
Tyler 12
Step 2: nothing, and maybe
nothing is a value: the value that says there is nothing here. It can be kept, handed around, and compared, and it speaks as nothing. Why would a program hold nothing on purpose? Because the honest answer to some questions is "it isn't there" — a lookup that might not find.
A maybe mark promises a value of one kind or nothing: maybe number is a number or nothing. Here is a lookup with an honest empty answer:
The book says it answers:
29 nothing
Look at how the nothing arrived: on the road where the name is not "Tyler", lookup reaches its end without give — so it hands back nothing. (Lesson 4's pocket, opened: a function that gives nothing back gives nothing back. The -> maybe mark makes that a promise instead of an accident.)
Your turn. Write ageOf the same way: it knows one person — "Ada" is 36 — and says nothing for anyone else. Say ageOf("Ada") and ageOf("Caleb").
The worked answer — try yours first, then peek
The book says it answers:
36 nothing
Step 3: handling both roads
A maybe value may not simply be used as if it were surely there — that is the whole point of marking it. The way to handle it is to ask, with == nothing, and the checker follows the asking: on the road where the answer was yes, the value is known to be nothing; on the other road, it is known to be there, of its kind, and may be used freely. The asking is the unwrapping — there is no other word to memorize:
The book says it answers:
Next year: 30 No age on file.
Your turn. Using your ageOf from Step 2, look up "Caleb" and handle both roads: if it is nothing, say "Unknown."; otherwise say the age doubled.
The worked answer — try yours first, then peek
The book says it answers:
Unknown.
Step 4: meet the checker
Now watch the checker work. This program is shown, not run, because the checker refuses it before anything runs:
make lookup(name) -> maybe number {
if name == "Tyler" {
give 29
}
}
let b = lookup("Noki")
say b + 1
Line 7: This uses a number or nothing — a value that
may be nothing — where a value that is surely there is
needed, and nothing is the empty value: there is
nothing there to use. Check the value first, like:
if value == nothing { ... } else { ... } — in the
'else', the value is known to be there. I caught this
before the program ran, so nothing ran.
Those are the checker's own words, from a real refusal. Read what it did for you: it read the whole program before running it, found the one line where a maybe value was used as if it were surely there, stopped everything, named the line, explained the problem in plain words, and showed the shape of the fix. That is safety you can feel — not a fence you crash into, but a friend at the door who reads your work and says "this one line, here; walk both roads." The guards stand behind the checker, too: a maybe that somehow slips past every proof and is used while it is nothing is refused at the moment of use, in the same plain words.
This is the Swift half of the promise, arriving early: strength that checks your work before it runs, the way a compiled language's does — while you are still writing as freely as JavaScript. You do not have to choose between the two halves. In Nifla they arrive together.
The ledger grows: finding a gift
The book learns to answer "what did we give to…?" — honestly, including when the answer is nothing is in the book:
The book says it answers:
Barry Cunningham: 500 cents No gift to Noki is in the book.
The mark says -> maybe object — a gift card, or nothing. Both lookups walk both roads, and the book never pretends.
Your turn
Write a phone book as a function numberFor that is handed a name and gives back maybe text: "Mom" gives "555-0100" and "Billy" gives "555-0125". Look up Billy and look up Quinton, handling both roads for each.
The worked answer — try yours first, then peek
The book says it answers:
Billy: 555-0125 No number for Quinton.
You can now…
- write kind marks on shelves, parameters, and
function promises, and have the language check them;
- say
nothing— the honest empty value — and promise
"this kind, or nothing" with maybe;
- handle both roads by asking
== nothing, the asking
being the unwrapping;
- read a checker refusal and know what it is telling
you: the line, the problem, the shape of the fix.
---