Constants

Basic constants

tape
const MAX_SIZE: i64 = 1024;
const PI: f64 = 3.14159265358979;
const APP_NAME: string = "MyApp";

Constants are evaluated at compile time and inlined at every usage site.

Computed constants with @tape

tape
@tape const CRC_TABLE: [256]u32 = {
    var t: [256]u32;
    for (i in 0..256) {
        var c: u32 = i as u32;
        for (j in 0..8) {
            if (c & 1 != 0) {
                c = 0xEDB88320 ^ (c >> 1);
            } else {
                c = c >> 1;
            }
        }
        t[i] = c;
    }
    return t;
};

The @tape prefix means the value is computed by running tape code at compile time.

Embedded file constants

tape
@tape const SHADER_SRC: string = @embed("shaders/blur.glsl");
@tape const ICON_DATA: []u8 = @embed("assets/icon.png");

@embed reads a file at compile time and stores its contents as a constant.

Last modified: