Built-in functions
These functions are part of the language: they need no import. Everything else is in the standard library.
| Function | Gives | Summary |
|---|---|---|
print(value) | writes a value and a line break | |
println(value) | the same as print | |
str(value) | string | a value as text |
input() | string | one line from standard input |
argc() | int | the number of command-line arguments, program name included |
argv(i) | string | argument i |
env(name) | string | an environment variable, or "" |
cwd() | string | the current directory |
exit(code) | ends the program with a status code | |
panic(message) | stops the program: a bug was found | |
assert(cond[, message]) | panics (or fails a test) when cond is false | |
assertEq(actual, expected[, message]) | the same, when the values differ | |
typename(value) | string | the class name of an object, or the type held by an any |
isInstance(object, Type) | bool | the same as object is Type |
nameof(Type) | string | a type's fully-qualified name, fixed at compile time (in generic code, the type T stands for) |
reflect(Class) | ClassInfo | a class's fields, methods and decorators at run time (import "std") |
int(x), long(x), float(x), byte(x), ... | conversions | |
sizeof(T) | int | a type's size in bytes |
free(x) | destroys an object now, or frees a pointer |
print(value)
println(value)Writes value and a line break to standard output. Any value that can be shown works: numbers, booleans, strings, enums, structs, fixed arrays, objects (through their toString() method, or their class name), collections, any, T? and null. Exactly one argument is taken; build a string to print several values. To print without a line break, use printRaw from the standard library; for standard error, IO.eprint.
struct Point { x: int y: int }
fn main() {
print(42)
print(Point { x: 1, y: 2 })
print([1.5, 2.0])
print("${1 + 1} things")
}42
Point { x: 1, y: 2 }
[1.5, 2]
2 thingsstr
str(value) -> stringThe text print would write, as a string. Floats use the shortest form that reads back as the same number.
fn main() {
let s = str(3) + "/" + str(true) + "/" + str(0.1 + 0.2)
print(s)
}3/true/0.30000000000000004input
input() -> stringReads one line from standard input, without the line break. At the end of input it returns "". IO.readLine() is the same; IO.readLines() reads everything.
argc and argv
argc() -> int
argv(i: int) -> stringargc() counts the command-line arguments including the program name, which is argv(0). With grav run file.grav -- a b, argc() is 3 and argv(1) is "a".
fn main() {
for (i in 1..argc()) {
print("argument ${i}: ${argv(i)}")
}
print("done")
}doneenv and cwd
env(name: string) -> string
cwd() -> stringenv returns an environment variable's value, or "" when it is not set. cwd returns the current working directory. OS in the standard library has more (OS).
exit
exit(code: int)Ends the program immediately with the status code (0 means success). Deferred code and finally blocks do not run.
panic
panic(message: string)Stops the program with panic: message and the call stack. Use it for situations that mean the program itself is wrong. A catch does not catch a panic; a recovery point (Panic.recover, an HTTP handler, a thread) does. See Errors.
assert and assertEq
assert(condition: bool[, message: string])
assertEq(actual, expected[, message: string])assert fails when condition is false, reporting the message and the file and line. assertEq fails when actual != expected and reports both values. Inside a test block, a failure fails that test; elsewhere, it panics.
assertion failed: math broke (app.grav:2)typename and isInstance
typename(value) -> string
isInstance(object, Type) -> booltypename gives the run-time class name of an object, or the name of the type an any holds. isInstance(obj, Cat) is the same as obj is Cat.
class Animal { }
class Cat extends Animal { }
fn main() {
let a: Animal = new Cat()
print(typename(a))
print(isInstance(a, Animal))
let v: any = 3.5
print(typename(v))
}Cat
true
floatnameof
nameof(Type) -> stringThe fully-qualified name of a type, known at compile time: a class, interface, struct, enum or primitive. Inside generic code, nameof(T) names the type T stands for in that instance, which makes it a key for registries such as a dependency-injection container (interfaces included, which reflect does not take). Unlike typename, it needs no value.
namespace app {
interface Store { fn get() -> string }
}
fn key<T>() -> string { return nameof(T) }
fn main() {
print(nameof(app.Store))
print(key::<app.Store>())
print(key::<int>())
}app.Store
app.Store
intConversions
int(x) long(x) uint(x) byte(x) int8(x) ... uint64(x) float(x) double(x) float32(x) bool(x) binary(x)Every built-in type name can be called like a function to convert, with the same rules as x as T and (T)x. When a function or variable of that name is in scope (say, your own fn double(x: int)), the name means yours. See Types.
fn main() {
print(long(3.9))
print(byte(300))
print(double(7) / 2)
print(uint(-1))
}3
44
3.5
4294967295sizeof
sizeof(Type) -> int
sizeof(expression) -> intThe size in bytes, as C's sizeof, fixed when compiling. sizeof(Type) of a class is the size of its objects' fields (sizeof(Point)); sizeof(expression) is the size of the value as the variable holds it, which for any object, collection or string is a reference: 8. Neither counts what an object holds elsewhere (an array's elements, a string's text): for the memory a value uses at run time, see Mem (Mem.sizeOf(x), Mem.deepSizeOf(x), Mem.describe(x)).
free
free(object)
free(pointer)On an object: runs its destructor (and its parents') now and clears its fields; the memory is released when the last reference goes, so other references never dangle. On a pointer: C's free. On a Future: releases a finished task's record. Objects are freed automatically, so free is only needed to release a resource at a precise moment. See Memory.
Library-internal built-ins
These are used inside the standard library; ordinary programs do not need them:
| Built-in | Purpose |
|---|---|
__retain(x), __release(x) | adjust the reference count of a value kept in raw memory |
__visit(x) | report a reference held in raw memory to the cycle collector |
__repr(x) | text of a value as shown inside a collection (strings quoted) |
__show(x) | text of a data-enum field |
__fmt(x, spec) | what ${x:spec} becomes |
__unwrap(x) | what x! becomes |