15. Builtins
Builtins start with @; any other name is E112.
| Builtin | Meaning |
|---|---|
@len(x) |
length of an array, a slice, a Buf or a str, as i64 |
@sizeOf(T) |
size of a value of type T in bytes, as i64 |
@typeName(T) |
the type’s name, as a str |
@hasDecl(T, "m") |
whether the type T declares the method m; only in an if condition, which it decides at compile time (section 14) |
@hasField(T, "f") |
whether the type T has the field f (a union’s member, an enum’s value); only in an if condition, as @hasDecl (section 14) |
@errorName(e) |
the name of the error e: anyerror, without error., as a str (section 13) |
@compileError("msg") |
E146 with that message wherever it is checked: everywhere except in a comptime if branch that is not chosen (section 14); msg must be a string literal |
@splat(v) |
an array with every element v; the array type comes from context (E115) |
@replace(&p, v) |
puts v in the place p and returns what was there (section 21) |
@take(&p) |
@replace(&p, Buf(T).empty) for a buffer place p (section 21) |
@swap(&xs, i, j) |
exchanges elements i and j of a buffer, array or inout slice (section 21) |
@forget(v) |
ends the owned struct v without running anything; only in the file that declares its type (section 21) |
@secureZero(&x), @secureZero(&x[lo..hi]) |
sets every byte of a place, or of a writable slice, to zero with stores the C compiler must keep, even when nothing reads the place again (where it may drop a plain assignment): for keys and other secrets, usually as defer @secureZero(&key); the place must own nothing (E103: free a Buf, after zeroing its bytes with &b[0..]) |
@mulWide(a, b) |
the whole product of two u64, which can need 128 bits, as [2]u64{ low, high }: a * b is low + high * 2^64; for bignum and field arithmetic, whose limbs are then 64 bits (* panics past 64 bits and *% drops the high half) |
@floatBits(x) |
the IEEE 754 bits of an f32 or f64, as a u32 or u64: a float as a map key (where -0.0 and 0.0 are two keys, and a NaN is one), or written out bit for bit |
@This() |
inside a struct or union: its own type |
@import("m.egg") |
another file as a module; only as const m = @import(...) at the top level (section 16, E129) |
@as(T, x) |
the number x as number type T; to an integer type it panics integer cast out of range (from an integer) or float cast out of range (from a float) if it does not fit (section 7) |
Builtins for pointers, which need allow unsafe (section 18):
| Builtin | Meaning |
|---|---|
@ptrAdd(p, n) |
p moved by n elements (n: i64, may be negative) |
@ptrCast(*U, p) |
the same address as a *U |
@intFromPtr(p) |
the address as a usize |
@ptrFromInt(*T, n) |
the address n: usize as a *T |
@strPtr(t) |
a *u8 to the bytes of the text t, a str, []u8 or Buf(u8) (not 0-terminated; do not write through it) |
@bufPtr(b) |
a *T to the elements of the buffer b: Buf(T), which may be written up to @len(b) |
@sliceFromPtr(p, n) |
the n: i64 elements at p: *T as a []T: an argument or a local slice only (E116); a negative n panics |
// expect: 7
// expect: 0 0
// expect: 5 0 0 5
uses io, alloc
fn main(io: Io, a: Allocator) void {
var key: [32]u8 = @splat(7)
defer @secureZero(&key) // zeroed when main returns
io.print(key[0])
var pair = [2]i64{ 3, 4 }
@secureZero(&pair)
io.print(pair[0], " ", pair[1])
var b = a.alloc(8, @as(u8, 5))
defer a.free(b)
@secureZero(&b[2..6])
io.print(b[1], " ", b[2], " ", b[5], " ", b[6])
}
// expect: 1 18446744073709551614
// expect: 6 0
uses io
fn main(io: Io) void {
const m: u64 = 18446744073709551615 // 2^64 - 1
const w = @mulWide(m, m) // 2^128 - 2^65 + 1
io.print(w[0], " ", w[1])
const small = @mulWide(2, 3)
io.print(small[0], " ", small[1])
}
v1’s @print is gone: printing is io, so it is io.print(x) with the io
capability (section 17). @print is E112.