Type casting

Operations for converting between types.

Overview

Instruction Signature Description
cast<T> (val -- T) Convert to specified type (runtime)

Conversions

cast

Converts a value to the specified type using the cast<T> syntax.

Signature: (val -- T)

Examples:

3.14 cast<i64>   // 3 (truncates toward zero)
42 cast<f64>     // 42
42 cast<d128>    // 42 (exact)
0.1 cast<d128>   // 0.1000000000000000055511151231257827: the f64's binary value
2.5d cast<f64>   // 2.5
42 cast<str>     // "42"

Supported types: i64, f64, d128, str, ptr

cast between numbers and to str always succeeds, so its result never has to be checked. cast<ptr> takes an i64 or a ptr. A struct or an array gives its raw address, and the ptr holds no reference to it, so it is only valid while something else keeps the value alive. A string cannot be cast to ptr: that is a runtime error. Use strings::data for a string's bytes.

A string cannot be cast to a number: parsing can fail and cast has nowhere to report it. Use strconv::atoi, strconv::parse_int, strconv::parse_float, strconv::parse_d128 or strconv::parse_bool, which are fallible.


Type system

Basic types

Type Description Size
i64 64-bit signed integer 8 bytes
f64 64-bit floating-point 8 bytes
d128 IEEE 754 decimal128: 34 decimal digits, written 0.1d 16 bytes
str String Variable
ptr Raw memory address 8 bytes
[]T Typed array (e.g., []i64, []f64, []str) 8 bytes (pointer)
fn(...) Function value (e.g., fn(i64 -- i64)) 8 bytes (reference)
bool Boolean (alias for i64) 8 bytes

Sized integer types

Quadrate's runtime stack holds 64-bit integers and floats, but struct fields and memory buffers can use narrower integer types to match binary layouts or pack data more tightly. Sized integers only matter at memory boundaries: a load sign-extends (signed) or zero-extends (unsigned) to i64 on the stack; a store truncates to the field's width.

Type Description Size
i8 / u8 8-bit signed / unsigned integer 1 byte
i16 / u16 16-bit signed / unsigned integer 2 bytes
i32 / u32 32-bit signed / unsigned integer 4 bytes
u64 64-bit unsigned integer 8 bytes

Use them as struct fields (x:u8, count:i32) or with mem::get_u8 / mem::set_i16 / etc. for raw byte-offset access. They are rejected anywhere else — a parameter, a return value, a module-level var, a cast target — because a stack value is always 64 bits, so there is no width for the annotation to describe. They used to be accepted there and do nothing: 300 cast<u8> yielded 300, not 44. To keep the low bits, mask with bits::and (value 255 bits::and, after use bits).

Note on u64: because the stack element is always an i64, a u64 field containing a value above 2^63-1 loads onto the stack as a negative i64 (same bit pattern). Arithmetic still works bitwise, but comparison operators treat it as signed. Use i64 when you don't specifically need the wider unsigned semantics at the memory layer.

Type declarations

In function signatures:

fn process(x:i64 y:f64 name:str data:[]i64 -- result:i64) {
    // ...
}

With function types:

fn apply(x:i64 f:fn(i64 -- i64) -- result:i64) {
    call
}

In struct fields:

struct Matrix {
    data:[]f64
    rows:i64
    cols:i64
}

Type aliases

The type keyword creates a name for an existing type:

type Transform = fn(i64 -- i64)
type IntList = []i64

An alias can be used where its underlying type is expected, and the other way round, for inputs and struct fields. Outputs are the exception for now: an array or function alias declared as an output is not matched by the value the body leaves (expects type 'IntArray' but got '[]i64'), so write array and function outputs with the type itself.

Type checking

Types are checked at compile time. The compiler verifies stack effects match declared signatures.

Implicit conversions

An i64 widens where it meets another number type, in arithmetic, in comparisons and as an argument: to f64 beside an f64, and to d128 beside a d128, where it is exact. An f64 and a d128 do not mix, because converting the f64 would bring in the binary rounding d128 exists to keep out; convert explicitly with cast<d128>.

fn main() {
    5 3.5 + print nl              // 8.5
    2 1.5d + print nl             // 3.5
    1.5 cast<d128> 2d + print nl  // 3.5
}

Nothing else converts implicitly: a string never becomes a number, and cast<T> is the explicit conversion.


ptr is raw memory

A ptr is an address the program allocated or was handed, with nothing counted behind it: mem::alloc, strings::data, a handle from a C library. A struct, an array, a string or a function value is never a ptr, and passing one where a ptr is taken is a compile error. A value keeps the type it was declared with, so there is nothing to narrow: declare a parameter with the struct type it takes.

struct Dog {
    name:str
}

fn get_name(d:Dog -- name:str) {
    <<name
}

fn main() {
    Dog { name = "Rex" } get_name print nl   // Rex
}

as Type, which narrowed a ptr to a struct, has been removed.