# 3.8 Enumerations

> An enumeration is a type with a closed list of named values, like the colours of a traffic light.

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

> **In one sentence**
>
> An enumeration is a type with a closed list of named values, like the colours of a traffic light.

## 3.8.1 How to write it

```pseudolearn
Enum «Name»
  ⟦«member», «member», …⟧
EndEnum
```

## 3.8.2 How it works

An **enumeration** declares a new type whose only values are the **members** of the list. Each member is written with the type's name, a dot and its name. A variable of that type can only hold one of those members, so there are no invented values and no typos that go unnoticed.

### Program: A named traffic light

```pseudolearn
Enum Semaforo
  Rojo, Amarillo, Verde
EndEnum
Algorithm Cruce
  Define luz As Semaforo <- Semaforo.Verde;
  If luz = Semaforo.Verde Then
    Write "Cruzar con ", luz;
  EndIf
EndAlgorithm
```

**Output**

```text
Cruzar con Verde
```

*The program finishes normally.*

**Figure 3.8-1: The members of an enumeration**

A box with the type Semaforo and its three members: Rojo, Amarillo and Verde. A variable luz, outside the box, points to the member Verde.

An enumeration is a closed list. A variable of the type can only point to one of its members.

Enumerations go very well with `Switch`, which can have one case per member:

### Program: One case per colour

```pseudolearn
Enum Semaforo
  Rojo, Amarillo, Verde
EndEnum
Algorithm Indicacion
  Define luz As Semaforo <- Semaforo.Amarillo;
  Switch luz Do
    Semaforo.Rojo: Write "Para";
    Semaforo.Amarillo: Write "Precaución";
    Semaforo.Verde: Write "Adelante";
  EndSwitch
EndAlgorithm
```

**Output**

```text
Precaución
```

*The program finishes normally.*

> **Limit**
>
> - Each member has an internal 32-bit ordinal from 0, in declaration order, but the program cannot convert it to a number.
> - An enumeration may be empty or have a single member.
> - It only accepts `=` and `<>`; it has no order and no arithmetic.
> - `Write` shows the member's name exactly as declared.

> **Common mistakes**
>
> A member that does not exist is detected before running:
>
> ### Program: A colour that does not exist
>
> ```pseudolearn
> Enum Semaforo
>   Rojo, Amarillo, Verde
> EndEnum
> Algorithm Azul
>   Define luz As Semaforo <- Semaforo.Azul;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `undefinedEnumMember` (error, 5:38): The enumeration has no member Azul
>
> *The program finishes with an error.*
>
> And members are not ordered:
>
> ### Program: Comparing with less than
>
> ```pseudolearn
> Enum Semaforo
>   Rojo, Amarillo, Verde
> EndEnum
> Algorithm OrdenEnum
>   Write Semaforo.Rojo < Semaforo.Verde;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `incompatibleOperandTypes` (error, 5:9): Incompatible types for operator <
>
> *The program finishes with an error.*

> **Under the hood**
>
> When running, a member is just its ordinal: a small integer in a slot. The name is recovered from the declaration when it needs to be written. Comparing two members means comparing two integers.
>
> **Intermediate representation**
>
> ```text
> module Cruce en
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @5:3-5:43
>   %0 = const.i64 2 @5:29-5:43
>   stmt.exit @5:3-5:43
>   stmt.enter @6:3-8:8
>   %1 = const.i64 2 @6:12-6:27
>   %2 = int.cmp eq, %0, %1 @6:10-6:11
>   decide.bool %2 @6:3-8:8
>   branch %2, b1(), b2()
> b1() mass 1:
>   stmt.enter @7:5-7:29
>   %3 = const.str "Cruzar con " @7:11-7:24
>   %4 = const.i64 0
>   %5 = int.cmp eq, %0, %4
>   branch %5, b5(), b6()
> b2() mass 0:
>   jump b3()
> b3() mass 0:
>   stmt.exit @6:3-8:8
>   return @9:1-9:13
> b4(%6: str) mass 0:
>   out.write %3, %6, newline @7:5-7:29
>   stmt.exit @7:5-7:29
>   jump b3()
> b5() mass 0:
>   %7 = const.str "Rojo"
>   jump b4(%7)
> b6() mass 0:
>   %8 = const.i64 1
>   %9 = int.cmp eq, %0, %8
>   branch %9, b7(), b8()
> b7() mass 0:
>   %10 = const.str "Amarillo"
>   jump b4(%10)
> b8() mass 0:
>   %11 = const.str "Verde"
>   jump b4(%11)
> }
> ```

**In short**
- An enumeration is a type with a closed list of members.
- Members are written `Type.member`.
- They only accept equality and inequality.
- They work very well with `Switch`.

**See also**
- [3.9 Variants](https://pseudolearn.app/en/spec/tipos/variantes.md)
- [6.2 Multiple selection with Switch](https://pseudolearn.app/en/spec/control/segun.md)
