Grav v1.14.0

Types

Every expression in Grav has a type known at compile time. This page lists every kind of type, how they convert into each other, and what their zero values are.

Built-in types

TypeSizeRange / meaningC type
int (int32)4 bytes−2,147,483,648 … 2,147,483,647int
long (int64)8 bytesabout ±9.22 × 10¹⁸long long
int8, int161, 2 bytessignedsigned char, short
uint (unsigned, uint32)4 bytes0 … 4,294,967,295unsigned int
ulong (uint64)8 bytes0 … about 1.8 × 10¹⁹unsigned long long
byte (uint8), uint161, 2 bytesunsignedunsigned char, unsigned short
float (double, float64)8 bytesIEEE 754 doubledouble
float324 bytesIEEE 754 singlefloat
bool1 bytetrue, falsebool
stringpointerimmutable UTF-8 text, reference countedconst char*
binarypointer + lengthraw bytesGravBytes
voidno value; only as a function resultvoid
any16 bytesa value of any type with a run-time tagGravAny

Composite types

WrittenMeaning
T[N]a fixed array of N elements, stored by value; N is a literal or a constant expression of consts declared above (int[SIZE * 2])
T[N][M]M arrays of N
T*a pointer to T
T?a nullable value: a T or null
fn(A, B) -> Ra function taking A and B and returning R; fn(A) returns nothing
Outer.Innera class, struct, enum or interface declared inside the class Outer (plain Inner inside it)
(A, B), (A, B, C), (A, B, C, D)tuples: the standard library's Pair<A, B>, Triple<A, B, C> and Quad<A, B, C, D>
Future<T>the result of an async call; await it for the T
Namea class, struct, enum, interface, trait or alias
Name<A, B>a generic type with type arguments
Selfinside a trait: the implementing class; a class method's result type: the receiver's own class; in a static method: the class the call names; Self.member in an instance method: the declaring class's static member

Variadic parameters are written ...name: T and behave like an array of T inside the function.

Value types and reference types

KindTypesAssigning / passing
valuenumbers, bool, enums, structs, fixed arrays, T? of those, pointerscopies the value
immutable sharedstring, binaryshares it (it cannot change)
referenceclasses, interfaces, data enums, function values, any holding an object, Futureshares the same object

Zero values

Fields a constructor does not set, and HashMap lookups of missing keys, give the type's zero value:

TypeZero value
numbers0
boolfalse
string""
enumsthe first member
structsevery field at its zero value
classes, interfaces, functions, pointers, T?, anynull

Assignability

A value of type From can be used where To is expected when:

FromToNotes
any typethe same type
an integer typeany integer typevalues that do not fit are truncated, as in C
an integer typeany float type
a float typea float type
a float typean integer typenot allowed: write int(x), x as int or (int)x
nulla class, interface, function, pointer or T?
a subclassa parent class
a classan interface it implements
TT?
T?Tnot allowed: use x!, x ?? fallback, or a null check
a numeric, bool, string, enum, class, interface, function or null value (or a T? of one)any
anya concrete typenot allowed: use x as T (checked at run time)

Everything else needs an explicit conversion, or is an error.

Conversions

T(x), x as T and (T)x all convert. T(x) works for every built-in type name (int(x), long(x), byte(x), double(x), ...), unless a function or variable with that name is in scope:

ConversionBehaviour
number → numberas in C; float to integer drops the fraction
number → bool0 is false, anything else true
integer → enum (n as Color)checked: a number that names no member throws ValueError
enum → integer (int(c))the member's value
string ↔ binarythe same bytes (shared, not copied); both index as bytes: s[i], b[i], and b.length
class → subclass, class or interface → interfacechecked at run time: throws TypeError
any → Tchecked at run time: stops with a message naming both types
T → anyboxes the value
anything printable → stringstr(x) (not as)

Text to number conversions are library functions: Format.

Nullable types

T? is allowed for numbers, bool, string, enums, and structs that contain no strings, objects, closures or any. For a class, interface, function or pointer type, T? is the same as T. A T? cannot be used as a T until it is unwrapped with x! or x ?? fallback, or narrowed by a null check. See Null safety.

Type inference

  • let x = value gives x the type of value. let x: auto = value is the same.
  • Integer literals are int unless they do not fit, then long.
  • Generic type arguments are inferred from call arguments, constructor arguments, lambda results and the expected type; auto in a type argument list asks for inference explicitly.
  • Lambda parameter types are inferred from the expected function type.
  • A block lambda without -> R takes its result type from its return statements.
  • [...] takes its type from the expected type: a fixed array, or a collection class.

Generic types

Generic classes, structs, interfaces, traits, aliases, functions and methods take type parameters in <...>. A parameter may have a bound, T: Bound, naming a class or interface the argument must be or implement. Each distinct list of type arguments compiles to its own code. See Generics.

Edit this page on GitHub