The standard librarySlide 56 of 86
56compilerThe standard library

Running a call while it builds

Grammar: with-expr
postfix-exprwithtimeoutadd-exprcompiler
with-expr = postfix-expr [ "with" ( "timeout" add-expr | "compiler" ) ] ;

f(x) with compiler runs the call once, while the program builds, and builds its answer into the executable in place of the call. Running the program then reads the value and does nothing else: no file is opened again, nothing is fetched again, nothing is worked out again.

  • Only the entrypoint, the file handed to hive build, may write one, so everything that runs on the building machine is named in that one file. The function it calls may come from any module.
  • Every argument is known before the program runs: a literal, a vector or a constructor of literals, or what another with compiler call answered — written in place, or through a name bound to it once with :=.
  • What it answers is built in, so it has to be data: numbers, text, vectors, maps, Results and declared types made of them. A function, a Secret or a type the library owns is refused.
  • It runs on every hive build, run and test, from the entrypoint's folder, before the program itself is compiled; hive check and the editor never run it. A panic in it is a compile error at the call, and what it prints is shown under the line it was written on.

It runs code while the program builds, so the tour's own server, which builds programs for the browser and runs none of a reader's code, refuses one.

A version, from git
// The version, read from git once, while the program builds.
func described(): Str {
	Result.Ok(said) := hive.term.exec("git", ["describe", "--tags"], ".") else {
		Result.Error(why) { panic why.message }
	}
	return trim(said)
}

proc main(): void {
	version := described() with compiler
	echo "running {version}"
}
A table, from a file
// countries.csv, beside this file: a code and a name on every row.
func countries(): hive.map.Map<Str, Str> {
	Result.Ok(rows) := hive.file.csv("countries.csv", ",") else {
		Result.Error(why) { panic why.message }
	}
	return hive.map.fromTable(rows)
}

proc main(): void {
	names := countries() with compiler
	if hive.map.get(names, "BR") is Result.Ok(name) {
		echo name
	}
	echo "{len(names)} countries, built in"
}