Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · XO0415

Docs

XO0415: const initializer performs an effect

error · reported by the checker

A top level const may call functions, but its initializer’s inferred effect set must be empty: no fs, net, clock, rand, env, proc, stdio, or ffi. It runs once, at the first read, in whichever task reads it first, so an effect there would happen at a time the program text does not show, and the value would depend on more than the program. Read the input in main and pass the value down. See core 3.1 and decision 0332.

Example

fn stamp() -> Int uses clock {
  42
}

const START = stamp()

Fix

fn stamp() -> Int uses clock {
  42
}

fn main(os: Os) {
  let start = stamp()
  os.stdio.println(start.display())
}

Run xo explain XO0415 for this text in a terminal, or see section 10.6 of the specification.