# 3.9 Variants

> A variant is a type whose values can take several different shapes, each with its own data, like a figure that can be a circle or a rectangle.

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

> **In one sentence**
>
> A variant is a type whose values can take several different shapes, each with its own data, like a figure that can be a circle or a rectangle.

## 3.9.1 How to write it

```pseudolearn
variant «Name»
  case «shape»(«field» As «type», …)
  case «shape»
endVariant
```

## 3.9.2 How it works

Each **alternative** is one possible shape of the value. An alternative may carry fields or none. A value is built with the variant's name, a dot and the alternative: `Forma.Circulo(2)`.

To find out which shape a value has, `Switch` is used with **patterns**: each branch names an alternative and gives names to its fields, which only exist inside that branch.

### Program: The area of each figure

```pseudolearn
Variant Forma
  Case Circulo(radio As Integer)
  Case Rectangulo(ancho As Integer, alto As Integer)
  Case Punto
EndVariant
Subroutine area(f As Forma) As Integer
  Define total As Integer <- 0;
  Switch f Do
    Forma.Circulo(r): total <- 3 * r * r;
    Forma.Rectangulo(a, h): total <- a * h;
    Forma.Punto: total <- 0;
  EndSwitch
  Return total;
EndSubroutine
Algorithm Areas
  Write area(Forma.Circulo(1));
  Write area(Forma.Rectangulo(3, 4));
  Write area(Forma.Punto);
EndAlgorithm
```

**Output**

```text
3
12
0
```

*The program finishes normally.*

**Figure 3.9-1: The shapes of a variant**

A box with the type Forma and its three alternatives drawn: a circle, a rectangle and a point. Below, Switch f Do distributes the value into three branches, one per alternative.

A variant is a box with several possible shapes. `Switch` looks at which shape it has and unpacks its fields.

`Switch` must cover **all** the alternatives or have `Otherwise`. That way, if a new alternative is added tomorrow, the engine points out every `Switch` that falls short.

### Program: A shape is forgotten

```pseudolearn
Variant Forma
  Case Circulo(radio As Integer)
  Case Punto
EndVariant
Algorithm Incompleto
  Define f As Forma <- Forma.Punto;
  Switch f Do
    Forma.Circulo(r): Write r;
  EndSwitch
EndAlgorithm
```

**Diagnostics**
- `nonExhaustiveSwitch` (error, 7:3): Missing branches: Punto

*The program finishes with an error.*

> **Limit**
>
> - A variant needs at least one alternative.
> - Alternative names are unique within the variant, and field names within each alternative.
> - A variant is a value: it is copied like a record.

> **Common mistakes**
>
> A variant without alternatives has no possible value:
>
> ### Program: An empty variant
>
> ```pseudolearn
> Variant Vacia
> EndVariant
> Algorithm P
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `variantWithoutAlternatives` (error, 1:9): Variant Vacia must have at least one alternative
>
> *The program finishes with an error.*

> **Under the hood**
>
> A variant value stores a tag with the alternative followed by that alternative's fields. `Switch` reads the tag and jumps straight to the matching branch.
>
> **Intermediate representation**
>
> ```text
> module Areas en
> func @area(%0: i64, %1: i64, %2: i64) -> i64 {
> b0() mass 2:
>   stmt.enter @7:3-7:31
>   %3 = const.i64 0 @7:30-7:31
>   stmt.exit @7:3-7:31
>   stmt.enter @8:3-12:12
>   %4 = const.i64 0 @9:5-9:21
>   %5 = int.cmp eq, %0, %4
>   branch %5, b3(), b6()
> b1(%6: i64) mass 1:
>   stmt.exit @8:3-12:12
>   stmt.enter @13:3-13:15
>   stmt.exit @13:3-13:15
>   return %6 @13:3-13:15
> b2() mass 0:
>   decide.none @8:3-12:12
>   jump b1(%3)
> b3() mass 2:
>   %7 = const.i64 0
>   decide.case %7 @8:3-12:12
>   stmt.enter @9:19-9:20
>   %8 = value.reinterpret i64, %1
>   stmt.exit @9:19-9:20
>   stmt.enter @9:23-9:41
>   %9 = const.i64 3 @9:32-9:33
>   %10 = int.mul %9, %8 @9:34-9:35
>   %11 = int.mul %10, %8 @9:38-9:39
>   stmt.exit @9:23-9:41
>   jump b1(%11)
> b4() mass 3:
>   %12 = const.i64 1
>   decide.case %12 @8:3-12:12
>   stmt.enter @10:22-10:23
>   %13 = value.reinterpret i64, %1
>   %14 = value.reinterpret i64, %2
>   stmt.exit @10:22-10:23
>   stmt.enter @10:25-10:26
>   %15 = value.reinterpret i64, %1
>   %16 = value.reinterpret i64, %2
>   stmt.exit @10:25-10:26
>   stmt.enter @10:29-10:43
>   %17 = int.mul %13, %16 @10:40-10:41
>   stmt.exit @10:29-10:43
>   jump b1(%17)
> b5() mass 1:
>   %18 = const.i64 2
>   decide.case %18 @8:3-12:12
>   stmt.enter @11:18-11:28
>   %19 = const.i64 0 @11:27-11:28
>   stmt.exit @11:18-11:28
>   jump b1(%19)
> b6() mass 0:
>   %20 = const.i64 1 @10:5-10:27
>   %21 = int.cmp eq, %0, %20
>   branch %21, b4(), b7()
> b7() mass 0:
>   %22 = const.i64 2 @11:5-11:16
>   %23 = int.cmp eq, %0, %22
>   branch %23, b5(), b8()
> b8() mass 0:
>   jump b2()
> }
> func suspendible @main() -> unit {
> b0() mass 3:
>   stmt.enter @16:3-16:31
>   %0 = const.i64 1 @16:28-16:29
>   %1 = const.i64 0
>   %2 = const.i64 0
>   %3 = value.reinterpret i64, %0
>   %4 = call @area(%2, %3, %1) @16:9-16:31
>   out.write %4, newline @16:3-16:31
>   stmt.exit @16:3-16:31
>   stmt.enter @17:3-17:37
>   %5 = const.i64 3 @17:31-17:32
>   %6 = const.i64 4 @17:34-17:35
>   %7 = const.i64 0
>   %8 = const.i64 1
>   %9 = value.reinterpret i64, %5
>   %10 = value.reinterpret i64, %6
>   %11 = call @area(%8, %9, %10) @17:9-17:37
>   out.write %11, newline @17:3-17:37
>   stmt.exit @17:3-17:37
>   stmt.enter @18:3-18:26
>   %12 = const.i64 0
>   %13 = const.i64 2
>   %14 = call @area(%13, %12, %12) @18:9-18:26
>   out.write %14, newline @18:3-18:26
>   stmt.exit @18:3-18:26
>   return @19:1-19:13
> }
> ```

**In short**
- A variant has several alternatives, each with its fields.
- It is built with `Variant.Alternative(…)`.
- It is queried with `Switch` and patterns that unpack the fields.
- `Switch` must cover every alternative.

**See also**
- [3.10 Optional and Result](https://pseudolearn.app/en/spec/tipos/opcional-resultado.md)
- [3.8 Enumerations](https://pseudolearn.app/en/spec/tipos/enumerados.md)
- [6.2 Multiple selection with Switch](https://pseudolearn.app/en/spec/control/segun.md)
