# 3.1 Primitive types

> Every piece of data has a type that says which values it can hold and what can be done with it; PseudoLearn has five primitive types and every other type is built from them.

Web page: https://pseudolearn.app/en/spec/tipos/primitivos

> **In one sentence**
>
> Every piece of data has a type that says which values it can hold and what can be done with it; PseudoLearn has five primitive types and every other type is built from them.

## 3.1.1 How to write it

```pseudolearn
Define «name» As Integer
Define «name» As Real
Define «name» As Boolean
Define «name» As Character
Define «name» As String
```

**Figure 3.1-1: The five primitive types**

Five boxes. Integer with the value 42; Real with 3.14; Boolean with its two values, True and False; Character with a single letter L; and String with the letters of Lucía in five boxes numbered 0 to 4.

The five primitive types. Two hold numbers, one a yes-or-no answer and two hold text: a single symbol or a row of symbols.

## 3.1.2 How it works

A variable is declared with `Define`, its name, the word `As` and its type. From that moment it only accepts values of that type, and its type does not change while the program runs.

### Program: Lucía's card

```pseudolearn
Algorithm FichaDeLucia
  Define edad As Integer <- 11;
  Define altura As Real <- 1.42;
  Define inicial As Character <- 'L';
  Define nombre As String <- "Lucía";
  Define aprobada As Boolean <- True;
  Write nombre + " (" + inicial + ")";
  Write edad, " años, ", altura, " m";
  Write aprobada;
EndAlgorithm
```

**Output**

```text
Lucía (L)
11 años, 1.42 m
true
```

*The program finishes normally.*

| Type | What it holds | Example values |
| --- | --- | --- |
| `Integer` | A number without decimals | `0`, `42`, `-7` |
| `Real` | A number with a decimal part | `3.14`, `0.5`, `-2.0` |
| `Boolean` | The answer to a yes-or-no question | `True`, `False` |
| `Character` | A single symbol | `'A'`, `"7"` |
| `String` | A row of symbols, even an empty one | `"Hola"`, `""` |

> **Analogy**
>
> Think of the type as the shape of a container: an egg box holds eggs and a bottle holds liquid. You cannot keep soup in the egg box even though both are "for keeping". The analogy stops working at one point: some values do move by themselves from one container to another, as long as nothing is lost.

## 3.1.3 `Integer`

An `Integer` is a number without a decimal part. Integer operations give the **exact** result as long as it fits in the range. Going outside the range does not wrap around silently: the program stops.

## 3.1.4 `Real`

A `Real` is a number with a decimal part. When written it always shows at least one decimal, so it can be told apart from an integer at a glance.

### Program: Reals show their point

```pseudolearn
Algorithm Precios
  Define precio As Real <- 7;
  Write precio;
  Write 0.1 + 0.2;
EndAlgorithm
```

**Output**

```text
7.0
0.30000000000000004
```

*The program finishes normally.*

The second line surprises almost everyone. It is not an engine bug: that is how reals work on any computer. A `Real` cannot hold just any number; it holds the nearest mark on a very fine ruler that still has gaps.

**Figure 3.1-2: The ruler with gaps**

A horizontal ruler with separate marks. The literal 0.3 points at one mark; the result of 0.1 plus 0.2 points at the neighbouring mark, 0.30000000000000004.

The ruler with gaps. 0.3 and 0.1 + 0.2 fall on neighbouring marks of the ruler, so they are not the same number.

## 3.1.5 `Boolean`

A `Boolean` has exactly two values, `True` and `False`. It is the type of every question: `edad >= 18` is not a number, it is a `Boolean`. It is not a number, it has no order and it does not turn into text by itself. When written it appears with the profile's word: `Verdadero` in Spanish and `true` in English.

## 3.1.6 `Character` and `String`

A `Character` is a single symbol: a letter, a digit, a sign, a space or an emoji. A `String` is a row of characters that may be empty. Positions inside a string count from zero.

### Program: Measuring and looking inside a text

```pseudolearn
Algorithm Textos
  Define cara As Character <- "😀";
  Write length(""), " ", length("año"), " ", charAt("Hola", 0);
  Write cara, " ", charCode(cara), " ", charFromCode(76);
EndAlgorithm
```

**Output**

```text
0 3 H
😀 128512 L
```

*The program finishes normally.*

## 3.1.7 Moving from one type to another

The language only converts a value on its own when no information can be lost: from `Integer` to `Real` and from `Character` to `String`. Everything else is requested with a function, because how to convert is the decision of whoever writes the program.

### Program: Requested conversions

```pseudolearn
Algorithm Conversiones
  Define nota As Real <- -8.7;
  Write trunc(nota), " ", round(nota), " ", toText(1.5) + "!";
EndAlgorithm
```

**Output**

```text
-8 -9 1.5!
```

*The program finishes normally.*

`trunc` cuts the decimals towards zero and `round` rounds to the nearest integer. With `-8.7` they give different results, which is why the language does not choose for you.

> **Limit**
>
> - `Integer`: 64 signed bits, from -9223372036854775808 to 9223372036854775807, both included. Going outside produces `integerOverflow`.
> - `Real`: 64-bit binary floating point (IEEE 754 binary64), always finite. Every operation rounds to the nearest representable value and, on a tie, to the one with an even mantissa. An infinity or a "not a number" stops the program with `nonFiniteRealResult`. Both 0.0 and -0.0 exist.
> - `Character`: a Unicode code point between 0 and 1114111, except the surrogates from 55296 to 57343.
> - `String`: no maximum length fixed by the language; it counts code points, not visible letters.

> **Common mistakes**
>
> Going past the maximum of an integer stops the program without writing anything:
>
> ### Program: Going past the maximum
>
> ```pseudolearn
> Algorithm Desborde
>   Define grande As Integer <- 9223372036854775807;
>   Write grande + 1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `integerOverflow` (error, 3:16): The operation produces an integer outside the signed 64-bit range
>
> *The program finishes with an error.*
>
> Comparing reals with `=` works, but the engine warns, because two calculations that should match may fall on neighbouring marks:
>
> ### Program: Comparing reals with =
>
> ```pseudolearn
> Algorithm Comparar
>   Define a As Real <- 0.1 + 0.2;
>   If a = 0.3 Then
>     Write "iguales";
>   Else
>     Write "distintos";
>   EndIf
> EndAlgorithm
> ```
>
> **Output**
>
> ```text
> distintos
> ```
>
> **Diagnostics**
> - `realEqualityComparison` (warning, 3:6): Equality comparison between real values; precision errors may yield unexpected results
>
> *The program finishes normally.*
>
> A number does not stick to a text with `+`; it has to be converted or separated with commas:
>
> ### Program: A number does not stick to a text
>
> ```pseudolearn
> Algorithm Mezcla
>   Write "Tengo " + 11;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `cannotConcatenateNumberWithText` (error, 2:9): Cannot concatenate number with text; use conversion function or separate expressions with comma
>
> *The program finishes with an error.*
>
> And a real does not fit into an integer on its own:
>
> ### Program: A real does not fit into an integer
>
> ```pseudolearn
> Algorithm SinPerder
>   Define cantidad As Integer <- 2.5;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `incompatibleTypesInAssignment` (error, 2:3): Incompatible types in assignment: cannot assign real to integer
>
> *The program finishes with an error.*

> **Under the hood**
>
> Before running, the engine translates the program into its intermediate representation. Each constant says what kind of value it produces: `i64` (64-bit integer), `f64` (real), `char`, `str` and `bool`. Since the type of every value is known when compiling, the virtual machine never asks "what are you?" at each step.
>
> **Intermediate representation**
>
> ```text
> module FichaDeLucia en
> func suspendible @main() -> unit {
> b0() mass 8:
>   stmt.enter @2:3-2:31
>   %0 = const.i64 11 @2:29-2:31
>   stmt.exit @2:3-2:31
>   stmt.enter @3:3-3:32
>   %1 = const.f64 1.42 @3:28-3:32
>   stmt.exit @3:3-3:32
>   stmt.enter @4:3-4:37
>   %2 = const.char 'L' @4:34-4:37
>   stmt.exit @4:3-4:37
>   stmt.enter @5:3-5:37
>   %3 = const.str "Lucía" @5:30-5:37
>   stmt.exit @5:3-5:37
>   stmt.enter @6:3-6:37
>   %4 = const.bool true @6:33-6:37
>   stmt.exit @6:3-6:37
>   stmt.enter @7:3-7:38
>   %5 = const.str " (" @7:18-7:22
>   %6 = text.concat %3, %5 @7:16-7:17
>   %7 = text.concat %6, %2 @7:23-7:24
>   %8 = const.char ')' @7:35-7:38
>   %9 = text.concat %7, %8 @7:33-7:34
>   out.write %9, newline @7:3-7:38
>   stmt.exit @7:3-7:38
>   stmt.enter @8:3-8:38
>   %10 = const.str " años, " @8:15-8:24
>   %11 = const.str " m" @8:34-8:38
>   out.write %0, %10, %1, %11, newline @8:3-8:38
>   stmt.exit @8:3-8:38
>   stmt.enter @9:3-9:17
>   out.write %4, newline @9:3-9:17
>   stmt.exit @9:3-9:17
>   return @10:1-10:13
> }
> ```
>
> The four fixed-size types fit in one machine slot; a string can be any length, so it lives on the heap and the slot only holds a reference.

> **Note**
>
> - "A number in quotes is a number": `"7"` is a `Character` and `"75"` a `String`. Predict the type before running.
> - "`/` between integers gives an integer": in PseudoLearn it always gives a real.
> - "0.1 + 0.2 = 0.3": it is not a quirk of the language; the ruler figure shows why it happens.

**In short**
- There are five primitive types: `Integer`, `Real`, `Boolean`, `Character` and `String`.
- The type of a variable never changes.
- An `Integer` is exact within its range; going outside stops the program.
- A `Real` holds the nearest mark on a finite ruler and always shows a decimal.
- A `Boolean` is not a number and has no order.
- Only `Integer` to `Real` and `Character` to `String` happen by themselves.

**See also**
- [2.6 Numeric literals](https://pseudolearn.app/en/spec/lexico/numeros.md)
- [3.2 Relations between types and widening](https://pseudolearn.app/en/spec/tipos/relaciones.md)
- [4.2 Division, Div and Mod](https://pseudolearn.app/en/spec/expresiones/division.md)
- [12.3 Conversions](https://pseudolearn.app/en/spec/biblioteca/conversion.md)
