Prelude functions
API summary (generated from the Beans source by npm run coverage): 8 package functions.
The prelude is the set of names available in every file without an import.
Besides the builtin types, it gives you a few free functions. This page covers
them. For the printing functions, which live in the io module, see the note
below and the standard library reference.
panic(message: string) stops the program for an error you cannot recover from. It
reports the call location and your message, exits with status 3, and never returns.
It does not run defers.
panic("index out of range")Use panic only for bugs that should never happen. For errors you can handle, use
Option and Result instead.
Compile-time layout functions
Section titled “Compile-time layout functions”These three return a compile-time constant for the target you are building for. They take a type, not a value, and give the layout of that type on the selected target.
| Function | Returns | Meaning |
|---|---|---|
size_of(Type) | int | how many bytes a value of Type takes |
align_of(Type) | int | the alignment of Type |
offset_of(Type, field) | int | the byte offset of field inside Type |
let s: int = size_of(i32)let a: int = align_of(f64)let o: int = offset_of(Point, x)Rules:
- These take a type in a contextual form, not a runtime value.
- They are rejected on type parameters, and on
Option,Result, and user enums, because those have no single layout. offset_ofneeds a struct or union type and a real field name.
See compile-time for more on constants known at build time.
Printing
Section titled “Printing”Printing is done with the io module, so you import it and call io.println:
import std.io
fn main() { io.println("hello")}The four printing functions are:
io.println: print a line to standard outputio.print: print without a newlineio.eprintln: print a line to standard errorio.eprint: print to standard error without a newline
What can print:
- numbers, bools, and strings
- enums, shown as
variantorvariant(payload) - lists of printable things, shown as
[a, b, c]
What cannot print directly:
Mapvalues and class instances do not print; give them a string form of your own.Resultis not printable;matchon it instead.
See also
Section titled “See also”- Option, Result, and Error,
panicalongside error handling. - Compile-time, build-time constants.
- The standard library reference, the
iomodule and otherstd.*functions.