Type System
Zynx features a statically checked type system optimized for performance, safety, and C interoperability.
Primitive Types
Integers
| Type | Size | Minimum | Maximum |
|---|---|---|---|
i8 | 8-bit | −128 | 127 |
i16 | 16-bit | −32,768 | 32,767 |
i32 | 32-bit | −2,147,483,648 | 2,147,483,647 |
i64 | 64-bit | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
i128 | 128-bit | −2¹²⁷ | 2¹²⁷ − 1 |
u8 | 8-bit | 0 | 255 |
u16 | 16-bit | 0 | 65,535 |
u32 | 32-bit | 0 | 4,294,967,295 |
u64 | 64-bit | 0 | 18,446,744,073,709,551,615 |
u128 | 128-bit | 0 | 2¹²⁸ − 1 |
isize | pointer-sized | platform-dependent | platform-dependent |
usize | pointer-sized | 0 | platform-dependent |
isize and usize are 32-bit on 32-bit platforms and 64-bit on 64-bit platforms.
Floating-Point
| Type | Size | Precision | Range (approx.) |
|---|---|---|---|
f16 | 16-bit | ~3 decimal digits | ±65,504 |
f32 | 32-bit | ~7 decimal digits | ±3.4 × 10³⁸ |
f64 | 64-bit | ~15 decimal digits | ±1.8 × 10³⁰⁸ |
f128 | 128-bit | ~34 decimal digits | ±1.2 × 10⁴⁹³² |
Built-in Types
| Type | Size | Description |
|---|---|---|
bool | 1 byte | true or false |
str | 16 bytes (slice) | Immutable UTF-8 string; .length returns scalar count, .size returns byte count |
void | 0 bytes | Absence of a value; used as function return type |
Memory Types
References (&T)
- Statically tracked borrows, either immutable (
const &T) or mutable (&T).
Unique Pointers (Unique<T>)
- Heap-allocated, single-owner value.
- Created with
Unique(value), which allocates and initializes the value on the heap. - Automatically freed when the owning scope ends.
- Supports auto-deref: fields and methods of
Tcan be accessed directly throughUnique<T>. - Implicitly coerces to
&Twhen passed where a reference is expected. - Has move semantics — assigning or returning a
Unique<T>transfers ownership. Unique<Unique<T>>andUnique<&T>are not allowed.
struct Buffer {
data: u8[256],
}
fn make_buffer() -> Unique<Buffer> {
return Unique(Buffer { data: [0; 256] });
}
fn main() {
let buf = make_buffer();
// buf.data is accessible via auto-deref
// buf is freed when main returns
}
Raw Pointers (*T)
- Native C-style pointers, nullable by default.
- Supports pointer arithmetic:
p + n,p - n.
Collections
Arrays and Slices
T[]: Slice (pointer + length) with bounds-checked indexing.T[N]: Sized array where the length is part of the type..length: Returns the number of elements.==/!=: Sized arrays of the same type and length support element-wise equality comparison.
Tuples
- Ordered collections:
(i32, str). - Supports destructuring:
let (x, y) = get_pair();.
Type Aliases and Distinct Types
Aliases (type)
- Transparent names for existing types.
type Buffer = u8[];
Distinct Types (distinct)
- Nominal types with the same runtime layout as their base type, but without implicit conversion.
type UserID = distinct u64;
let user = 42 as UserID;
let raw = user as u64; // Explicit cast required
Custom Types
Structs
- Defined with
struct. Support fields, methods, and visibility attributes (@private,@readonly). - See Structs.
Enums
- Defined with
enum. Variants can carry data. - See Enums.
Error Unions (T!)
- Holds either a value of type
Tor an error. - Handled via
tryandcatch. - See Error Handling.