# 11.4 Effects and capabilities

> To touch the outside world —the console, files, the clock, the network— a program asks for a **capability**: a key that gives access to one single thing.

Web page: https://pseudolearn.app/en/spec/tareas/efectos

> **In one sentence**
>
> To touch the outside world —the console, files, the clock, the network— a program asks for a **capability**: a key that gives access to one single thing.

## 11.4.1 How it works

A **capability** is an opaque value that stands for a specific permission. There is one family per resource:

| Capability | What for |
| --- | --- |
| `Console` | writing to the output and the error output, setting the exit code |
| `Arguments` | reading the program's arguments, with `requestArguments()` |
| `Environment` | reading environment variables, with `requestEnvironment()` |
| `DirCap`, `FileCap` | folders and files |
| `Clock`, `Timer` | the time and waits |
| `NetworkCap` | HTTP requests |

They are requested with a `request…` function and return a `Result`: the host grants the family if it supports it, or returns an `IoError`. With `Propagate` the error is simply returned to the caller.

**Figure 11.4-1: Capabilities as keys**

On the left, the program with three capabilities: Console, FileCap and Clock. On the right, the host with the terminal, the disk and the time. Each capability is an arrow to its resource.

The program does not touch the host directly: every resource is used through its key, which the host handed over when asked.

### Program: Warning through the error output

```pseudolearn
Subroutine Avisar() As Result<Integer, IoError>
  Define consola As Console <- Propagate requestConsole();
  Discard consola.writeError("Falta el nombre\n");
  Return consola.setExitCode(2);
EndSubroutine

Algorithm Revisar
  Write "Revisando";
  Switch Avisar() Do
    Success(codigo): Write "Código de salida: ", codigo;
    Failure(problema): Write problema.code;
  EndSwitch
EndAlgorithm
```

**Output**

```text
Revisando
Falta el nombre
Código de salida: 2
```

*The program finishes normally.*

Capabilities cannot be forged or manipulated: each one carries a slot, a generation and some rights. They can be **narrowed** —a subdirectory, a single web origin— but never widened. Closing a capability invalidates it: using it afterwards gives the error `CapacidadCerrada`.

> **Limit**
>
> - Effects are only performed from a `Subroutine` or the algorithm, never from a [`Function`](#subprogramas.pureza).
> - Analysis does not ask the host: which families it supports is discovered when running.

**In short**
- A capability is the key to an outside resource.
- It is requested with `request…` and arrives as a `Result`.
- It can be narrowed but not widened; once closed, it no longer works.

**See also**
- [12.7 Files and folders](https://pseudolearn.app/en/spec/biblioteca/archivos.md)
- [12.10 Time and dates](https://pseudolearn.app/en/spec/biblioteca/tiempo.md)
- [12.9 HTTP](https://pseudolearn.app/en/spec/biblioteca/http.md)
- [7.8 Purity: Function versus Subroutine](https://pseudolearn.app/en/spec/subprogramas/pureza.md)
