Home / Learn

Release v0.4.0

3 Text and conditions

55 min readEdition 0.2Builds on1 Hello, Una2 NumbersDiagnosticsU0006U0007U0101U2001U5001

Page 1 of 10

Most of what a store's program shows to people is text: a product name, a SKU, a line on a label, a note that says "sold out". And most of what it decides is a question with a yes or no answer: is this product on sale? Is the shelf empty? Does the name fit on the label? This chapter teaches both. You learn how Una writes and builds text, how to clean, search and cut it, how to ask yes-or-no questions with Bool, and how to choose between two values with if. You end with a program that prints the shelf labels of a store.

What you'll build

The shelf labels of a store. The program cleans each SKU, cuts each name to the width of a label, writes the price, the sale price or the price per kilogram, adds a note when a shelf runs low, and asks for a check when a SKU does not look right:

Output
LAMP-01 | Desk lamp, brass | 19.90
RUG-07 | Wool rug, 120 by 180 cm, | 45.00, now 38.25 (15% off) | only 2 left
COFFEE-05 | Coffee beans, dark roast | 12.50 (25.00 per kg)
DESK-02 | Oak writing desk | 159.00 | sold out
Tea mug, blue: check the SKU "mug 4"

You will learn to:

  • write texts, with escapes for the characters that need them, and build texts from values with interpolation;
  • clean, search, replace and cut texts with the functions every module has, called with a dot;
  • ask yes-or-no questions with comparisons and Bool, and join them with and, or and not;
  • choose between two values with if … then … else …;
  • read what the checker says when a cut or a division may go wrong, and guard it.

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

Text and conditions are where many programs make quiet mistakes. In some languages, + joins a text and a number into a new text without a word, so that "Total: " + 1 + 2 gives Total: 12, where the reader meant 3. A number or an empty text can stand for "no" in a condition, so a slip that tests the amount instead of the rule still runs, and answers the wrong question. A cut of a text at a position past its end stops the program while a customer waits, or quietly gives back less than the code expected.

Una makes each of these a rule that una check enforces before the program runs. A text and a number are different types, and nothing turns one into the other unless the code says so, with interpolation. A condition must be a Bool, true or false, and nothing else is read as one. A cut states what it needs, positions inside the text, and the checker proves that every cut in the program gets them, or points at the one that may not, with an example of the text that would break it. if is a choice between two values of one type, so every branch gives a value the rest of the code can use.

Key idea

In Una, a text is a text, a condition is a Bool, and a cut of a text is proved to stay inside it before the program runs.

View the Markdown

Next:3.1 Text