4 Kinds
Page 1 of 10
So far every value has been a single number, text or condition. A store's data comes in larger shapes. A parcel has an order number, a weight, a zone and a place where the customer collects it. A zone is one of a few fixed choices. A place may be a locker, which has a name, or the door, which has none. This chapter shows how to declare such shapes yourself, as kinds, so that the checker knows them as well as it knows Int and Text. You end with a program that prices parcels from their kinds.
What you'll build
A price list for the day's parcels: each parcel's charges for its zone, its weight, its speed and its delivery, then its total, and what express shipping would cost for the first one. The program prints:
Parcel A-1001, 1200 g
National: 450 cents
up to 2 kg: 150 cents
Standard: 0 cents
locker Station 14: 0 cents
total: 600 cents
Parcel A-1002, 3500 g
Local: 300 cents
over 2 kg: 400 cents
Express: 500 cents
to the door: 250 cents
total: 1450 cents
Parcel A-1003, 400 g
Abroad: 1200 cents
up to 500 g: 0 cents
Express: 900 cents
store Harbour Road: 0 cents
total: 2100 cents
Express for A-1001: 600 cents now, 1100 cents expressYou will learn to:
- declare a record kind, build its values, and read their fields;
- declare a sum kind, whose values are each one of a fixed list of variants;
- test a variant with
is, and take a value apart withwhen; - write a generic kind and a generic function, and the constraint it needs.
Download this chapter's packages: each step is a package under steps/, and the chapter's program is day-to-day/; run una check . and una run . in one.
Why it works this way
Many programs keep a parcel's status as a text, such as "in_transit", next to fields that are empty in most states: a tracking number that only a parcel in transit has, a delivery date that only a delivered one has. Nothing stops a parcel that is "delivered" with no date, or "packed" with a tracking number. A typo such as "in_trnasit" is a new status that no code knows. And when the store adds a status, every choice among the statuses still runs, and the new status quietly falls into whatever case comes last.
Una lets you give each shape a type of its own:
- A record is a type of its own. A
Parcelhas exactly its fields, every one of them is given when a parcel is built, and aParcelis never mistaken for another record with the same fields. - A sum lists every case. A
ZoneisLocal,NationalorAbroad, and nothing else. Each case carries only its own data: a locker delivery has a locker's name, and a door delivery has none. whencovers every case. Awhenover aZonehas an arm for each zone, or anelse. Add a zone, and the checker points at everywhenthat needs a new arm.
The price is a few declarations at the top of a module. In return, a value that makes no sense cannot be built, and a change to the shape of the data shows every place it touches.
Declare the cases your data can be in as the variants of a sum, each carrying only what is true in that case. Then a value that makes no sense cannot be built, and no code can forget a case.
Next:4.1 Records