Home / Learn

Release v0.4.0

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:

Output
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 express

You 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 with when;
  • 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:

  1. A record is a type of its own. A Parcel has exactly its fields, every one of them is given when a parcel is built, and a Parcel is never mistaken for another record with the same fields.
  2. A sum lists every case. A Zone is Local, National or Abroad, and nothing else. Each case carries only its own data: a locker delivery has a locker's name, and a door delivery has none.
  3. when covers every case. A when over a Zone has an arm for each zone, or an else. Add a zone, and the checker points at every when that 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.

Key idea

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.

View the Markdown

Next:4.1 Records