Contents

Haze has a small set of scalar types. Their ranges and conversion rules are fixed, and the compiler does not silently narrow a value. This chapter is the reference for choosing a scalar type and for converting between them.

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 the i64 range 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 the target type at compile time. A constant that does not fit is an error, not a wrapped value.

var a u8 = 255       // accepted
var b u8 = 256       // compile-time error: does not fit in u8
var c i8 = -128      // accepted

Arithmetic Promotion#

Arithmetic unifies the operand types. Narrow values are promoted to at least i32 or u32, and the result follows the wider operand. Signed and unsigned operands are unified by rank, but mixing them is discouraged because the result may not be the type a reader expects.

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. The compiler does not insert a hidden truncation.

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 useful 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; a 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. A cast from a signed negative value to an unsigned type is allowed with cast, and the result follows the two's-complement bit pattern; use that operation only when the bit pattern is the intended meaning.

Floating Point#

f64 is the only floating-point type. It follows IEEE-754 double precision, including infinities and NaN. Floating-point literals contain a decimal point or an exponent.

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

Floating-point values support +, -, *, /, and the ordering and equality comparisons. They do not support %, bitwise operators, or shifts. A comparison with NaN follows IEEE semantics and is false for all ordering and equality operators.

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")
}

The equality operators compare booleans directly, and the only ordering relationship a program should rely on is equality. The language does not define false < true.

Void#

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

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

A void function may still 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 only valid when the target is strictly narrower than the source. truncate is an accepted 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 happens to fit. This keeps the spelling honest: the reader sees that information may be lost.

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, identifiers, and values that may exceed the 32-bit range. Use unsigned types for bit patterns and externally defined unsigned quantities, not as a substitute for validation. Use f64 for measurements and scientific calculation, and avoid exact equality comparisons on computed floating-point values. Use a refinement type when the meaningful constraint is narrower than the primitive's range; the next chapters show how.