Grav v1.14.0

Built-in functions

These functions are part of the language: they need no import. Everything else is in the standard library.

FunctionGivesSummary
print(value)writes a value and a line break
println(value)the same as print
str(value)stringa value as text
input()stringone line from standard input
argc()intthe number of command-line arguments, program name included
argv(i)stringargument i
env(name)stringan environment variable, or ""
cwd()stringthe 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)stringthe class name of an object, or the type held by an any
isInstance(object, Type)boolthe same as object is Type
nameof(Type)stringa type's fully-qualified name, fixed at compile time (in generic code, the type T stands for)
reflect(Class)ClassInfoa class's fields, methods and decorators at run time (import "std")
int(x), long(x), float(x), byte(x), ...conversions
sizeof(T)inta type's size in bytes
free(x)destroys an object now, or frees a pointer

print

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.

grav
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")
}
Output
42
Point { x: 1, y: 2 }
[1.5, 2]
2 things

str

str(value) -> string

The text print would write, as a string. Floats use the shortest form that reads back as the same number.

grav
fn main() {
    let s = str(3) + "/" + str(true) + "/" + str(0.1 + 0.2)
    print(s)
}
Output
3/true/0.30000000000000004

input

input() -> string

Reads 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) -> string

argc() 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".

grav
fn main() {
    for (i in 1..argc()) {
        print("argument ${i}: ${argv(i)}")
    }
    print("done")
}
Output
done

env and cwd

env(name: string) -> string
cwd() -> string

env 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) -> bool

typename 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.

grav
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))
}
Output
Cat
true
float

nameof

nameof(Type) -> string

The 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.

grav
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>())
}
Output
app.Store
app.Store
int

Conversions

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.

grav
fn main() {
    print(long(3.9))
    print(byte(300))
    print(double(7) / 2)
    print(uint(-1))
}
Output
3
44
3.5
4294967295

sizeof

sizeof(Type) -> int
sizeof(expression) -> int

The 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-inPurpose
__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

Edit this page on GitHub