Spec chapters

4. Declarations and names

const name = expr          // immutable; type inferred
const name: T = expr       // immutable; type checked against T
var name = expr            // mutable
var name: T = expr

Rules, all checked at compile time:

  1. Every name is declared before use within its block (E101). Functions and types are the exception: they are visible everywhere in their file, and in other files through an import (section 16).
  2. A local lives until the end of the block that declares it.
  3. No shadowing. A local or parameter may not reuse the name of any visible local, parameter, function, type or import (E106).
  4. const and parameters cannot be assigned (E105), nor can their fields or elements. Copy a parameter into a var to change it, or make it inout (section 12).
  5. Every local must be read at least once (E110). Assigning to it, or to one of its fields, does not count as reading it.
  6. Every var must be changed at least once after its declaration (E111): assigned, have a field or element assigned, or be passed as inout. If it never changes, it must be const.

Rules 5 and 6 follow Zig: an unused or never-changed variable is a sign the code does not say what it means, so it is rejected rather than tolerated.

// error: E111
uses io

fn main(io: Io) void {
    var x = 1          // never changed: must be const
    io.print(x)
}
// error: E106
uses io

fn main(io: Io) void {
    const x = 1
    if (x > 0) {
        const x = 2    // shadows the outer x
        io.print(x)
    }
}

Type aliases

const Name = T at the top level of a file gives the type T a second name. T is any type, and it means what it means where the alias is written, wherever the alias is used. Aliases are visible everywhere in their file and through an import, like types.

// expect: 3 2
// expect: 7
uses io, alloc

const Bytes = Buf(u8)
const Id = i64
const Op = fn(i64, i64) i64
const Dot = Point              // may come before what it names
const Point = struct {
    x: i64
    y: i64

    fn sum(self: Point) i64 {
        return self.x + self.y
    }
}

fn add(x: i64, y: i64) i64 {
    return x + y
}

fn main(io: Io, a: Allocator) void {
    var b: Bytes = .empty
    defer a.free(b)
    a.push(&b, 1)
    a.push(&b, 2)
    const f: Op = add
    const n: Id = f(1, 2)
    io.print(n, " ", @len(b))
    io.print(Dot{ .x = 3, .y = 4 }.sum())
}

An alias is the same type, not a new one: a value of Id is an i64, they mix without @as, and messages name the type itself. Everything about the type carries over, so an alias of an owned struct can no more build one than any other file can (E142).

A literal may use an alias of a struct or union (Dot{ .x = 1, .y = 2 }), but an array literal is always written [N]T{ ... } or [_]T{ ... }: an alias cannot stand for that prefix. An alias cannot take type parameters: use a type function (section 14). Aliases that go round in a circle are E119; a struct may reach itself through one (const Kids = Buf(Node), with kids: Kids in Node).

// error: E119
const A = B
const B = A

fn main() i64 {
    return 0
}

Constants

const NAME = value at the top level of a file names a value known when compiling: a number, bool, text or enum literal, another constant, or integer arithmetic on those (+ - * / % << >> & | ^, -, ~), which the compiler does on i64 values, with C’s division (E150 for anything else, such as a call, and for a zero divisor of / or %, shifting by less than 0 or more than 63, or a result or step outside i64; a u64 or usize above i64 is written as one literal, const MAX: u64 = 18446744073709551615, which a float constant may also negate). A type may follow the name, const OK: i32 = 0: a number type, bool, str or an enum (E150 for any other); without one an integer is an i64 and a float an f64, as for a local const, and an enum literal needs one. The value must fit the type (E003). Constants are visible everywhere in their file and through an import (limits.KB), like functions, and a constant may name one declared after it, but not itself (E150). const B = A makes B a constant when A is one, and an alias when A is a type. Every constant is checked, used or not. A constant cannot be assigned or passed as inout (E105), and no local or parameter may take its name (E106).

// expect: 1048576 255 -2
// expect: egg true
uses io

const KB = 1024
const MB = KB * KB
const MASK: u8 = 0xF0 | 0x0F
const NAME = "egg"
const ON = true

fn main(io: Io) void {
    io.print(MB, " ", MASK, " ", -5 / 2)
    io.print(NAME, " ", ON)
}