Contents

The compiler does not silently narrow a value.

Integer Types#

Type Width Range
i8 8-bit signed −128 to 127
i16 16-bit signed −32,768 to 32,767
i32 32-bit signed −2,147,483,648 to 2,147,483,647
i64 64-bit signed −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
u8 8-bit unsigned 0 to 255
u16 16-bit unsigned 0 to 65,535
u32 32-bit unsigned 0 to 4,294,967,295
u64 64-bit unsigned 0 to 18,446,744,073,709,551,615

i32 is the default integer type for arithmetic and for literals that fit in 32 bits. A decimal literal above the i32 range but within i64 has type i64. A literal above the signed 64-bit range has type u64. Hexadecimal literals follow the same rule.

const small = 42
const wide = 9_000_000_000
const huge = 18446744073709551615

A constant is checked against its target type at compile time.

var a u8 = 255       // accepted
var b u8 = 256       // compile-time error
var c i8 = -128      // accepted

Arithmetic Promotion#

Narrow values are promoted to at least i32 or u32, and the result follows the wider operand. Mixing signed and unsigned operands follows rank rules; avoid it in application code.

Operation Result
i8 + i8 i32
i16 * i16 i32
i32 + i32 i32
i32 + i64 i64
u8 + u8 u32
u32 + u32 u32
u32 + u64 u64

Assigning a result to a narrower type requires an explicit conversion.

var a i32 = 10
var b i64 = 20
var c i64 = cast[i64](a) + b
var d i32 = trunc[i32](c)

Unsigned Values#

Unsigned types are for bit manipulation, lengths, and values that are genuinely non-negative. They are not a substitute for a refinement type when the domain requires a positive value; u32 still includes zero. Unary minus is rejected on unsigned values.

var mask u8 = 255
var total u64 = 0
print(mask & 15)
print(~mask)

A negative constant assigned to an unsigned type is rejected. Casting a signed negative value to an unsigned type follows the two's-complement bit pattern.

Floating Point#

f64 is the only floating-point type. It follows IEEE-754 double precision, including infinities and NaN.

const pi = 3.141592653589793
const tiny = 1.0e-9
const sum = 1.5 + 2.25

Floating-point values support +, -, *, /, ordering, and equality. They do not support %, bitwise operators, or shifts. Comparison with NaN follows IEEE semantics.

print(1.0 / 0.0)      // inf
print(0.0 / 0.0)      // nan
print(1.5 < 2.5)      // true

Booleans#

bool has the values true and false. It is required for if, for, &&, ||, and !. There is no implicit conversion from integers or pointers to bool.

const ready = true
if ready && !cancelled {
    print("go")
}

Ordering of booleans is not defined.

Void#

void is the absence of a value. It may appear as a function return type. It may not be used for a parameter, variable, list element, option payload, or generic argument.

fn log(message str) {
    print(message)
}

A void function may use return without an expression. A non-void function must return a value on every path that can complete normally.

Numeric Conversion#

cast[T](value) converts between numeric types and widens integers. trunc[T](value) narrows an integer and is valid only when the target is strictly narrower. truncate is a synonym for trunc.

Source Target Form Result
i32 i64 cast[i64](x) Sign-extended
u32 u64 cast[u64](x) Zero-extended
i64 i32 trunc[i32](x) Low 32 bits interpreted as i32
i32 u8 trunc[u8](x) Low 8 bits interpreted as u8
i32 f64 cast[f64](x) Numeric conversion
f64 i32 cast[i32](x) Fractional part discarded
u64 f64 cast[f64](x) May lose precision above 2^53

A narrowing cast without trunc is rejected even when the source is a constant that fits.

const narrowed = trunc[u8](300)     // 44
const widened = cast[i64](narrowed)
const rounded = cast[i32](3.9)      // 3

Choosing a Type#

Use i32 for ordinary counts and loop variables. Use i64 for sizes, timestamps, and identifiers. Use unsigned types for bit patterns and externally defined unsigned quantities, not as a substitute for validation. Use f64 for measurements and avoid exact equality on computed floating-point values. Use a refinement type when the constraint is narrower than the primitive's range.