Skip to content

SpecificationIII · Types and values

3.3

Inference: the type is always declared

In PseudoLearn no variable guesses its type: each one is declared with Define and an explicit type before it is used.

Markdown version

3.3.1 How it works

Some languages deduce the type of a variable from the first value it receives. PseudoLearn does not: the declaration is required and the type is always written. The initial value is optional and never decides the type.

Figure 3.3-1.First the box is declared with its shape; then values can be stored. An assignment never creates a new box.

A Define total As Integer line creates an empty box named total. Below, the assignment total gets 10 stores a value in that box. A note recalls that assigning to an undeclared name is an error.

ProgramDeclare and then assign

Algorithm Declarar
  Define total As Integer;
  total <- 10;
  total <- total * 2;
  Write total;
EndAlgorithm

Output

20

The program finishes normally.

Assigning to a name nobody declared does not create a variable: it is an error. That way a typo such as contdor instead of contador is found before running, instead of silently creating a new variable.

ProgramAssigning without declaring

Algorithm SinDeclarar
  contador <- 1;
EndAlgorithm

Diagnostics

  • undeclaredVariable · error · 2:3 · Undeclared variable: contador

The program finishes with an error.

In short

  • Every variable is declared with Define and an explicit type.
  • The initial value is optional and does not decide the type.
  • Assigning to an undeclared name is an error, not a declaration.