Error codes

E0246: return, ? or fail in a guard or constant

In a terminal: auk explain E0246 prints this page.

On this page

A when guard only decides whether a clause or arm applies; it must be pure and can't fail or return. The same goes for a module constant's initializer.

Wrong#

auk
fn positive(s: str): bool when int.parse(s)? > 0 = true
fn positive(_: str): bool = false

Fixed#

auk
fn positive(s: str): bool when (int.parse(s) else 0) > 0 = true
fn positive(_: str): bool = false

Common causes#

  • ? on a fallible call in a guard: give a fallback with else.
  • return inside a guard: compute the condition in the body instead.
  • A fallible constant: compute it in main and pass it where it's needed.

Stuck on a word? Try the glossary. Got a compiler error? Look up its code. Improve this page