---
title: "2.1 Values, types and names: let"
url: https://una-lang.org/en/learn/numbers/values-types-and-names-let/
release: v0.4.0
edition: "0.2"
---

# 2.1 Values, types and names: let

## 2.1.1 What a value is

A **value** is one piece of data that a program works with: the number `18`, the text `"LAMP-01"`. A program computes new values from old ones, as `18 - 9` gives `9`, and prints some of them.

Every value has a **type**, which says what kind of value it is, and so what you can do with it. You can multiply two numbers, but not two product names. Think of the boxes of a paper form: one box takes a date, another a phone number, and a clerk refuses a form with a name in the date box. Types are those boxes, and `una check` is the clerk.

The types of this chapter:

| Type | Its values | Written as |
| --- | --- | --- |
| `Int` | whole numbers: positive, zero or negative | `18`, `1_990`, `-500` |
| `Text` | a sequence of characters, as in chapter 1 | `"Desk lamp, brass"` |
| `Unit` | one value only, which carries no information; `main` gives it back | `unit` |

A value written out in the code, such as `18` or `"LAMP-01"`, is a **literal**. [Section 2.3](https://una-lang.org/en/learn/numbers/fixed-width-integers/#s2-3) adds the fixed-width integers, and [chapter 3](https://una-lang.org/en/learn/text-and-conditions/conditions-bool/#s3-3) adds `Bool`, the type of yes-or-no answers.

## 2.1.2 Why every value has a type

`print` takes a text. Here is a program that prints when the store closes, and a version that gives `print` the number `18` instead:

```diff include="steps/01-closing..steps/02-print-number:src/closing.una" title="src/closing.una"
--- a/src/closing.una
+++ b/src/closing.una
@@ -4,4 +4,4 @@
 pub main() -> Unit
   uses @console: Console[print]
 =
-  @console.print("The store closes at 18:00")
+  @console.print(18)
```

```text include="steps/02-print-number/expected/rendered.txt"
[U2001] Error: argument `line` of operation `Console.print` must be `Text`, found `Int`
   ╭─[ src/closing.una:7:18 ]
   │
 7 │   @console.print(18)
   │                  ─┬
   │                   ╰── argument `line` of operation `Console.print` must be `Text`, found `Int`
   │
   │ Note: run `una explain U2001` for an explanation
───╯
```

The message says that the argument of `print`, which is called `line`, must be a `Text`, and that the value given is an `Int`. Some languages would turn the number into a text without a word, and some would fail only when the line runs. Una refuses before the program runs, and it never turns a value of one type into another unless the code says so. Saying so takes two braces, as [Putting values into a text](#putting-values-into-a-text) shows.

## 2.1.3 How to write values and names

### 2.1.3.1 Literals

- An `Int` literal is written in decimal digits: `18`, `1990`. An `_` may group the digits, so `1_990` is 1990, easier to read. For a negative number, write a minus sign in front: `-500`. [Section 2.2](https://una-lang.org/en/learn/numbers/whole-numbers-int/#s2-2) gives every rule.
- A `Text` literal is written between double quotes, on one line, as in chapter 1: `"Desk lamp, brass"`. [Chapter 3](https://una-lang.org/en/learn/text-and-conditions/text/#s3-1) covers texts in full.
- The one `Unit` value is written `unit`. You rarely write it: a function whose result type is `Unit`, such as `main`, gives it back on its own.

### 2.1.3.2 Naming a value: `let`

`let` gives a name to a value, so that the steps after it can use the value by its name:

- `let lamps = 2` names the value `2` as `lamps`. The type, `Int`, comes from the value.
- `let lamp_cents: Int = 1_990` writes the type too, after `:`. It is optional, and it tells a reader what the name holds. When it is written, `una check` holds the value to it.

A `let` is a step of a body, on a line of its own, like a call of `print`. Such a name is also called a **binding**: it binds a word to a value. A name of a value follows the rules of a function's name: small letters, digits and `_`, starting with a letter, with no `__` and no `_` at the end: `lamps`, `lamp_cents`, `sku2`.

### 2.1.3.3 Putting values into a text

A name or a computation in braces, inside a text, puts its value into the text: `"{lamps} x {sku}"` is the value of `lamps`, then ` x `, then the value of `sku`. This is an **interpolation**. An `Int` goes in as plain decimal digits, with a minus sign when it is negative, and with no `_`. A computation may stand in the braces too: `"{lamps * lamp_cents} cents"`.

That is how a program prints a number: it puts the number into a text, and prints the text. [Chapter 3](https://una-lang.org/en/learn/text-and-conditions/text/#s3-1) shows the rest of what braces can hold.

## 2.1.4 Each step is a package

Each example of this chapter, and of every chapter after it, is a package of its own, made as in [chapter 1, Making the package](https://una-lang.org/en/learn/hello/your-first-program-hello-world/#making-the-package): a directory with `una.toml`, `verification.lock` and the module under `src/`. The book calls these packages the chapter's **steps**. Each one has a name such as `steps/03-values`: the number orders the steps in the download.

You can type each step, or take it whole from the chapter's download, `02-numbers.tar.gz`, which holds every package of this chapter. To make a step by hand next to the package of chapter 1, start in your home directory. A new terminal starts there already; if your terminal is still in `hello`, go up to your home directory with `cd ..` (`..` is the path of the directory above). Then make the new directory as before:

```sh
cd ..
mkdir values
cd values
mkdir src
```

Then write `una.toml`, `src/values.una` and `verification.lock` with your editor. `verification.lock` is the same in every package of this book. The manifest names the package and the module that `una run .` starts, here `values`, and grants the console, as in chapter 1.

## 2.1.5 Values in a store

This program names four values, two texts and two numbers, and prints them and what it computes from them:

:::steps
```una include="steps/03-values/src/values.una" title="steps/03-values/src/values.una" verified="check, run"
module values

--- Prints a few values of two types, named with `let`, and what is computed from them.
pub main() -> Unit
  uses @console: Console[print]
=
  let sku = "LAMP-01"
  let name: Text = "Desk lamp, brass"
  let lamps = 2
  let lamp_cents: Int = 1_990
  @console.print(sku)
  @console.print(name)
  @console.print("{lamps}")
  @console.print("{lamps} x {sku}")
  @console.print("{lamps * lamp_cents} cents for {lamps} lamps")
  let total = lamps * lamp_cents
  @console.print("Total: {total} cents")
```

1. **`let sku = "LAMP-01"`.** A name for a `Text`. A **SKU**, a stock keeping unit, is the code a store gives each product.
2. **`let name: Text = "Desk lamp, brass"`.** A `Text` with its type written out.
3. **`let lamps = 2`.** A name for an `Int`.
4. **`let lamp_cents: Int = 1_990`.** An `Int` with its type written out. Money is kept in **cents**, whole numbers, so that nothing ever rounds by accident; [section 2.2](https://una-lang.org/en/learn/numbers/whole-numbers-int/#s2-2) says why.
5. **`@console.print(sku)`.** A name of a `Text` can go to `print` as it is.
6. **`@console.print("{lamps}")`.** A number goes into a text first: the text holds only the number.
7. **`"{lamps * lamp_cents} cents for {lamps} lamps"`.** A computation and a name inside one text.
8. **`let total = lamps * lamp_cents`.** A name for a computed value. The computation runs once, at this line, and `total` holds its result.
:::

`una run .` prints:

```text include="steps/03-values/expected/output.txt"
LAMP-01
Desk lamp, brass
2
2 x LAMP-01
3980 cents for 2 lamps
Total: 3980 cents
```

## 2.1.6 Opening hours

Names make a computation read like the sentence it stands for. This program prints the store's hours from two numbers:

:::steps
```una include="steps/04-opening/src/opening.una" title="steps/04-opening/src/opening.una" verified="check, run"
module opening

--- Prints today's opening hours, and what follows from them.
pub main() -> Unit
  uses @console: Console[print]
=
  let opens = 9
  let closes = 18
  @console.print("Open today from {opens}:00 to {closes}:00")
  let hours = closes - opens
  @console.print("That is {hours} hours of trading")
  let cutoff = closes - 1
  @console.print("Orders placed before {cutoff}:00 ship today")
```

1. **`let opens = 9` and `let closes = 18`.** The two hours that everything else comes from.
2. **`"Open today from {opens}:00 to {closes}:00"`.** A text with two interpolations. The `:00` after each brace is plain text.
3. **`let hours = closes - opens`.** A computed value, 9, named for what it means.
4. **`let cutoff = closes - 1`.** The last hour at which an order ships the same day.
:::

Its manifest names the package and its module `opening`:

```toml include="steps/04-opening/una.toml" title="steps/04-opening/una.toml"
[package]
name = "opening"
version = "0.1.0"
edition = "0.2"

[run]
entry = "opening"
grants = ["console"]
```

`una run .` prints:

```text include="steps/04-opening/expected/output.txt"
Open today from 9:00 to 18:00
That is 9 hours of trading
Orders placed before 17:00 ship today
```

When the store changes its closing time, one line changes, `let closes = 18`, and the three printed lines follow it.

## 2.1.7 How values and names behave

### 2.1.7.1 A value keeps its type

No value turns into another type on its own. An `Int` becomes part of a text only where the code puts it in braces, and a text never turns into a number on its own. A name with a written type holds the value to that type. Write the price as a text, `let lamp_cents: Int = "1990"`, and `una check .` reports:

```text include="steps/07-declared/expected/rendered.txt#L1-L6"
[U2001] Error: `lamp_cents` is declared as `Int`, found `Text`
    ╭─[ src/values.una:10:25 ]
    │
 10 │   let lamp_cents: Int = "1990"
    │                         ───┬──
    │                            ╰──── `lamp_cents` is declared as `Int`, found `Text`
```

The quotes make `"1990"` a `Text`, four characters, not the number. The message names the binding, the type it was given, and the type it got.

### 2.1.7.2 A name exists from its line on

The steps of a body run in order, from the top, and a name exists only from the `let` that makes it. Move `let hours` of `opening` one line down, under the line that prints it:

```text include="steps/06-before/expected/rendered.txt#L1-L6"
[U1001] Error: cannot find name `hours` in this module, its imports or the prelude
    ╭─[ src/opening.una:10:28 ]
    │
 10 │   @console.print("That is {hours} hours of trading")
    │                            ──┬──
    │                              ╰──── cannot find name `hours` in this module, its imports or the prelude
```

At line 10 the name `hours` does not exist yet, so `una check .` cannot find it. The repair is to make the name before the step that uses it.

### 2.1.7.3 A name is made once

A body makes each name once. Reuse a name for a new value, as `let lamps = lamps * lamp_cents` would in `values`, and `una check .` points at both lines:

```text include="steps/05-twice/expected/rendered.txt"
[U1002] Error: `lamps` is already bound in this scope; a local cannot rebind another
    ╭─[ src/values.una:16:7 ]
    │
  9 │   let lamps = 2
    │       ──┬──
    │         ╰──── first defined here
    │
 16 │   let lamps = lamps * lamp_cents
    │       ──┬──
    │         ╰──── `lamps` is already bound in this scope; a local cannot rebind another
    │
    │ Note: run `una explain U1002` for an explanation
────╯
```

In the message, a **scope** is the part of the code where a name exists, here the body of the function, and a **local** is a name made inside it with `let`. If one name could hold two values, a reader would have to know which line they stand on to know what it means. Give the new value a name of its own, such as `total`.

### 2.1.7.4 A name keeps its value

A name made with `let` holds its value to the end of the body: no later step can give it another. A count that changes as a program works, such as a running total, uses `let edit` and an assignment, which [chapter 9](https://una-lang.org/en/learn/loops/names-that-change-let-edit/#s9-2) teaches with loops. Every name in this chapter keeps its value.

## 2.1.8 When to name a value

:::decide
**Which to use:**

| You want to… | Use |
| --- | --- |
| Use a value in several steps of one body | `let`, once, then its name |
| Give a computation a name that says what it means | `let hours = closes - opens` |
| State what type a name holds, for the reader and the check | `let lamp_cents: Int = 1_990` |
| Use the same value in several functions | a function that gives it back, as in [chapter 1](https://una-lang.org/en/learn/hello/writing-a-function/#s1-4) |
| Print a number | an interpolation: `"{lamps}"` |
:::

## 2.1.9 Check yourself: values and names

A program counts the places in the boxes of an order of mugs:

```una include="steps/08-names/src/names.una#main" title="steps/08-names/src/names.una"
--- Prints the labels of a box of mugs.
pub main() -> Unit
  uses @console: Console[print]
=
  let mugs = 6
  let per_box = 4
  let boxes = 2
  let places = boxes * per_box
  let spare = places - mugs
  @console.print("{mugs} mugs in {boxes} boxes")
  @console.print("{spare} empty places")
  @console.print("{places - spare} of {places} places taken")
```

What does `una run .` print?

:::answer Show the answer
```text include="steps/08-names/expected/output.txt"
6 mugs in 2 boxes
2 empty places
6 of 8 places taken
```

`places` is 2 × 4, 8, and `spare` is 8 − 6, 2. The last line computes `places - spare` inside the braces: 8 − 2 is 6.
:::

One more, without code. With `lamps` an `Int` and `sku` a `Text`, which of these steps check?

1. `@console.print(lamps)`
2. `@console.print("{lamps}")`
3. `let both = lamps * sku`
4. `@console.print("{lamps} x {sku}")`

:::answer Show the answer
Steps 2 and 4 check. Step 1 gives `print` an `Int` where it takes a `Text`: U2001, as in [Why every value has a type](#why-every-value-has-a-type). Step 3 multiplies a number by a text, which has no meaning, and the check reports U2001 for it too. In steps 2 and 4 the braces put the values into a text, so `print` gets the `Text` it takes.
:::
