Skip to content

SpecificationIII · Types and values

3.10

Optional and Result

Optional represents a value that may be missing and Result an operation that may go well or badly; both force you to look at the bad case before using the value.

Markdown version

3.10.1 How to write it

Define «name» As Optional<«type»> <- Some(«value»);
Define «name» As Optional<«type»> <- None;
Define «name» As Result<«type», «error type»> <- Success(«value»);
Define «name» As Result<«type», «error type»> <- Failure(«error»);

3.10.2 How it works

They are two built-in variants:

  • Optional<T> has two shapes: Some(value) when there is a value and None when it is missing.
  • Result<T, E> has two shapes: Success(value) when the operation went well and Failure(error) when it did not.

Like any variant, they are queried with Switch. There is no way to "take the value out and hope it is there": the program has to decide what to do in both cases.

ProgramA division that may fail

Subroutine dividir(a As Integer, b As Integer) As Result<Integer, String>
  If b = 0 Then
    Return Failure("no se divide entre cero");
  EndIf
  Return Success(a Div b);
EndSubroutine
Algorithm Division
  Switch dividir(10, 2) Do
    Success(cociente): Write "Cociente: ", cociente;
    Failure(motivo): Write "Error: ", motivo;
  EndSwitch
  Switch dividir(1, 0) Do
    Success(cociente): Write "Cociente: ", cociente;
    Failure(motivo): Write "Error: ", motivo;
  EndSwitch
EndAlgorithm

Output

Cociente: 5
Error: no se divide entre cero

The program finishes normally.

Figure 3.10-1.Two boxes that may or may not be full. To open them, both possibilities must be considered.

Two columns. Optional can be Some with a value or None. Result can be Success with a value or Failure with a reason. Below, a note says both cases must be considered with Switch, and that Propagate passes the error upwards.

3.10.3 Propagate

Inside a subroutine that also returns Result (or Optional), Propagate avoids writing a Switch for every call: if the value is Success, it gives its content; if it is Failure, it ends the subroutine returning that same error.

ProgramPassing the error upwards

Subroutine dividir(a As Integer, b As Integer) As Result<Integer, String>
  If b = 0 Then
    Return Failure("no se divide entre cero");
  EndIf
  Return Success(a Div b);
EndSubroutine
Subroutine mitadDelCociente(a As Integer, b As Integer) As Result<Integer, String>
  Define c As Integer <- Propagate dividir(a, b);
  Return Success(c Div 2);
EndSubroutine
Algorithm Encadenar
  Switch mitadDelCociente(20, 0) Do
    Success(valor): Write valor;
    Failure(motivo): Write "Falló: ", motivo;
  EndSwitch
EndAlgorithm

Output

Falló: no se divide entre cero

The program finishes normally.

Discard makes it clear that you ignore a Result on purpose: Discard dividir(4, 2);.

In short

  • Optional<T>: Some(value) or None.
  • Result<T, E>: Success(value) or Failure(error).
  • They are queried with Switch; there is no shortcut that ignores the bad case.
  • Propagate returns the error upwards; Discard ignores it on purpose.