Error codes › optional-read

Error codes

optional-read

error Behaviour: states, actions and tests

What it means#

A body reads an optional field (position: optional float32) as if it were always there: absent, it would be a silent zero. Read it with if let p = msg.position { … } (the arm runs when it is there, p its value), guard let p = msg.position else { return } (p from there on) or msg.position ?? 0.0 (its value, else the default). The quick fix writes ?? <zero of the type>; it is offered in the editor, archec fix leaves it, as the default is yours to choose. Setting the field (msg.position = 1.5) needs none. An error from edition 1.1; before it (and with no edition) a warning, and the body reads the field as it is.

How to fix it#

The message says what is wrong and where, and often what was meant (Did you mean …?). Change the model so that what this page describes no longer holds, then check again: archec check main.arche.

An error: archec builds nothing until it is fixed.

archec check --beginner says the most common messages in plain words, with the names that exist.

The automatic fix#

archec has no fix of its own for this code. When the message says Did you mean …?, VS Code offers that change (Quick Fix).

In the terminal#

archec explain optional-read prints this explanation; archec explain behaviour lists the codes of its area.