std/prelude
std/prelude re-exports the most commonly used functions from the standard library. These symbols are available in every Zap program without any import statement.
Automatically available symbols
Section titled “Automatically available symbols”The following functions work without any import:
// From std/ioprintln(s: String) VoidprintInt(i: Int) VoidprintFloat(f: Float) VoidprintBool(b: Bool) VoidprintChar(c: Char) Voideprintln(s: String) VoidgetLn() StringThat’s why simple programs like this work without any imports:
fun main() Int { println("Hello, Zap!"); printInt(42); return 0;}Importing prelude explicitly
Section titled “Importing prelude explicitly”You can also import std/prelude to get additional re-exported symbols:
import "std/prelude" { println, printInt, cwd, join, toString };
fun main() Int { println(cwd()); println(join("/tmp", "output.txt")); println(toString(42)); printInt(42); return 0;}Re-exported modules
Section titled “Re-exported modules”std/prelude pulls in APIs from:
| Source module | What’s re-exported |
|---|---|
std/io | println, printInt, printFloat, printBool, printChar, eprintln, getLn |
std/process | cwd, exit, argc, argv |
std/path | join, basename, parent |
std/string | len, fromChar, pushChar, eq |
std/convert | toString, toInt, toFloat, toBool, toChar |
std/error | Error, ErrorKind, Err, Kind |
std/math | abs, min, max, sqrt, floor, ceil |
Example — using prelude functions
Section titled “Example — using prelude functions”import "std/prelude" { println, printInt, toString, abs };
fun main() Int { var n: Int = -42; println("Absolute value of " ~ toString(n) ~ " is " ~ toString(abs(n))); printInt(abs(n)); // 42 return 0;}