# 3.7 Records

> A record groups several named pieces of data into a single value, like a card with its sections; assigning it copies it whole.

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

> **In one sentence**
>
> A record groups several named pieces of data into a single value, like a card with its sections; assigning it copies it whole.

## 3.7.1 How to write it

```pseudolearn
Record «Name»
  Define «field» As «type»;
  …
EndRecord
```

## 3.7.2 How it works

A **record** is declared outside the algorithm, with a name and its **fields**. After that it is used like any other type: it is built by writing its name with one value per field, in the order they were declared, and each field is read or changed with a dot.

### Program: A student's card

```pseudolearn
Record Alumno
  Define nombre As String;
  Define nota As Real;
EndRecord
Algorithm Ficha
  Define ana As Alumno <- Alumno("Ana", 8.5);
  ana.nota <- ana.nota + 1;
  Write ana.nombre, ": ", ana.nota;
EndAlgorithm
```

**Output**

```text
Ana: 9.5
```

*The program finishes normally.*

## 3.7.3 It is copied by value

A record is a **value**, not a reference. Assigning it, passing it to a subroutine or returning it copies all its fields. Changing the copy does not change the original.

### Program: Each variable has its own card

```pseudolearn
Record Casilla
  Define fila, columna As Integer;
EndRecord
Algorithm Tablero
  Define a As Casilla <- Casilla(1, 2);
  Define b As Casilla <- a;
  b.fila <- 10;
  Write a.fila, " ", b.fila, " ", a = b;
EndAlgorithm
```

**Output**

```text
1 10 false
```

*The program finishes normally.*

**Figure 3.7-1: Assigning a record copies it**

Two cards, a and b, with the fields fila and columna. Both had fila 1 and columna 2; after changing b.fila to 10, only b shows 10 and a still has 1.

Assigning a record makes a copy. Both cards start equal, but from then on they change separately.

Two records of the same type are equal if all their fields are. A record cannot contain itself, directly or indirectly.

> **Limit**
>
> - At least one field; at most 65535 leaves (fields that are not records, counting nested ones).
> - A record with more than 1024 leaves cannot be an array element.
> - Allowed fields: primitive types, enumerations, classes and other records.
> - Writing a whole record is not allowed; its fields are written.

> **Common mistakes**
>
> A record is not written in one go:
>
> ### Program: Writing the whole card
>
> ```pseudolearn
> Record Casilla
>   Define fila, columna As Integer;
> EndRecord
> Algorithm EscribirRegistro
>   Write Casilla(1, 2);
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `recordCannotBeWritten` (error, 5:3): A record cannot be written directly
>
> *The program finishes with an error.*
>
> And building one needs a value per field:
>
> ### Program: A field is missing
>
> ```pseudolearn
> Record Casilla
>   Define fila, columna As Integer;
> EndRecord
> Algorithm Faltan
>   Define c As Casilla <- Casilla(1);
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `argumentCountMismatch` (error, 5:26): Incorrect number of arguments: expected 2 but found 1
>
> *The program finishes with an error.*

> **Note**
>
> In the Spanish profile `y` cannot be the name of a field: it is the reserved word for "and", and reserved words ignore case.

> **Under the hood**
>
> In the machine, a record takes several consecutive slots, one per leaf. Copying it means copying those slots, without creating anything on the heap. That is why small records are as fast as loose variables.
>
> **Intermediate representation**
>
> ```text
> module Ficha en
> func suspendible @main() -> unit {
> b0() mass 3:
>   stmt.enter @6:3-6:45
>   %0 = const.str "Ana" @6:34-6:39
>   %1 = const.f64 8.5 @6:41-6:44
>   stmt.exit @6:3-6:45
>   stmt.enter @7:3-7:27
>   %2 = const.i64 1 @7:26-7:27
>   %3 = int.to_real %2 @7:26-7:27
>   %4 = real.add %1, %3 @7:24-7:25
>   stmt.exit @7:3-7:27
>   stmt.enter @8:3-8:35
>   %5 = const.str ": " @8:21-8:25
>   out.write %0, %5, %4, newline @8:3-8:35
>   stmt.exit @8:3-8:35
>   return @9:1-9:13
> }
> ```

**In short**
- A record groups named fields into a single value.
- It is built with one value per field and accessed with a dot.
- It is copied when assigned, passed or returned.
- Two records are equal if all their fields are.

**See also**
- [9.1 Classes and attributes](https://pseudolearn.app/en/spec/objetos/clase.md)
- [9.6 Copies, references and identity](https://pseudolearn.app/en/spec/objetos/copia.md)
- [5.4 Designators](https://pseudolearn.app/en/spec/declaraciones/designadores.md)
