Variables and types

Declaring variables

A variable is written as name : type = value:

count : i32 = 0;

If you skip the type, it comes from the value:

other := count; // other is i32

Every variable must have a initial value. This is an error:

x : i32; // compile error

Basic types

A number can have its type at the end:

a := 123i32;
b := 10u64;
c := 1.5f; // f32

A number without a type is a compile time number. Math on such numbers is done by the compiler. When it is stored into a real type, the compiler checks that the value fits without loss.

Zero and garbage

0 sets any value to zeros (zeroing memory):

s : MyStruct = 0;

If you really want to skip the initial value (for speed), say it out loud with #memory_garbage:

s : MyStruct = #memory_garbage;

Mox follows idea of ZII - Zero Is Initialization. So better design your data in a way that zero is a valid default state.

Structs

struct MyStruct {
    x: i32;
};

The memory layout of a struct is the same as in C.

Struct values are written with .{ }. Fields can be named. Use , or ; between them:

a : MyStruct = .{ x = 123 };
b := MyStruct.{ x = 123, };
b = .{ x = 123; };

Fields could also come just in order:

b := MyStruct.{ 123 };

When the type is known, you can write just .{ ... }. Otherwise put the type in front: MyStruct.{ ... }.

Enums

enum Direction {
    Left;
    Right;
};

enum Flags : u8 {
    A := 0;
    B := 2;
};

d := Direction.Right;
d : Direction = .Right; // same as Direction.Right

By default an enum uses the smallest integer type that fits all values. You can set it with : u8 and set values with :=.

Arrays

An array holds its items inside itself. When you copy an array, all items are copied.

a : [3]i32 = .{ 1, 2, 3 };    // fixed size
b : [?]i32 = .{ 0, 1, 2, 3 }; // size comes from the value

a.ptr gives a pointer to the first item.
a.len gives length of an array.

It is “synthetic” fields that works as operators, in memory only items are stored for fixed arrays.

Slices

A slice is a pointer and a length, like struct { ptr: *T; len: i64; }. When you copy a slice, only the pointer and length are copied, not the items.

s : []i32 = ...;
s.ptr; // pointer to first item
s.len; // number of items
s[0];  // first item

Strings

A string literal can be one of two things, based on the type:

a : []u8 = "a slice of u8";            // slice to constant memory
b : *u8 = "a C string, ends with zero"; // C string

Most Mox code uses []u8. Use *u8 when you call C functions.

String literals are null terminated but Mox code relies on string slices without null termination.
In case you need to pass string to some C API that expects null terminated string, use #temp_c_string.

Constants

const SIZE : i64 = 10;
const NAME := "mox";

A constant value lives at compile time. Its initial value can be any expression, and it is evaluated while compiling.

Global variables

A variable declared outside of functions is global. Its initial value is also evaluated at compile time, the same way as for const. The result is stored in the program, so no code runs for it at start:

TABLE_SIZE : i64 = 16 * 1024;
greeting : []u8 = "hello";

Unlike a constant, a global can be changed while the program runs.

Add #thread_local to give each thread its own copy of a global:

frame_count : i64 = 0; #thread_local

Two special types

Mox has two more built-in types. They are used for compile time code:

You will see them in Generics and Code generation.