3. Aggregate Types
Arrays
Fixed-size arrays with value semantics.
def SIZE = 5;
let ints: int[SIZE] = [1, 2, 3, 4, 5];
let zeros: [int; SIZE]; // Zero-initialized
int ints = ;
int zeros = ;
Tuples
Group multiple values together, access elements by index.
let pair = (1, "Hello");
let x = pair.0; // 1
let s = pair.1; // "Hello"
typedef struct Tuple_int_string;
Tuple_int_string pair = ;
int x = pair.v0; // 1
const char *s = pair.v1; // "Hello"
Multiple Return Values
Functions can return tuples to provide multiple results:
fn add_and_subtract(a: int, b: int) -> (int, int) {
return (a + b, a - b);
}
let result = add_and_subtract(3, 2);
let sum = result.0; // 5
let diff = result.1; // 1
typedef struct Tuple_int_int;
Tuple_int_int
Tuple_int_int result = ;
int sum = result.v0; // 5
int diff = result.v1; // 1
Destructuring
Tuples can be destructured directly into variables:
let (sum, diff) = add_and_subtract(3, 2);
// sum = 5, diff = 1
Tuple_int_int tmp = ;
int sum = tmp.v0;
int diff = tmp.v1;
// sum = 5, diff = 1
Typed tuple destructuring allows explicit type annotations:
let (a: string, b: u8) = ("hello", 42);
let (x, y: i32) = (1, 2); // Mixed: x inferred, y explicit
const char *a = "hello";
uint8_t b = 42;
int x = 1;
int32_t y = 2;
Structs
Data structures with optional bitfields.
struct Point {
x: int;
y: int;
}
// Struct initialization
let p = Point { x: 10, y: 20 };
// Bitfields
struct Flags {
valid: U8 : 1;
mode: U8 : 3;
}
typedef struct Point;
// Struct initialization
Point p = ;
// Bitfields
typedef struct Flags;
Structs use Move Semantics by default. Fields can be accessed via . even on pointers (Auto-Dereference).
Opaque Structs
You can define a struct as opaque to restrict access to its fields to the defining module only, while still allowing the struct to be allocated on the stack (size is known).
// In user.zc
opaque struct User {
id: int;
name: string;
}
fn new_user(name: string) -> User {
return User{id: 1, name: name}; // OK: Inside module
}
// In main.zc
import "user.zc";
fn main() {
let u = new_user("Alice");
// let id = u.id; // Error: Cannot access private field 'id'
}
// In user.h
typedef struct User;
User ;
// In user.c
User
// In main.c
int
Enums
Tagged unions (Sum types) capable of holding data.
enum Shape {
Circle(float), // Holds radius
Rect(float, float), // Holds width, height
Point // No data
}
typedef enum Shape_Tag;
typedef struct Tuple_float_float;
typedef struct Shape;
Shape
Shape
Shape
Unions
Standard C unions (unsafe access).
union Data {
i: int;
f: float;
}
typedef union Data;
SIMD Vectors
Native SIMD vector types using GCC/Clang vector extensions. Annotate a struct with @vector(N) to define a vector of N elements.
import "std/simd.zc";
fn main() {
let a = f32x4{v: 1.0}; // Broadcast: {1.0, 1.0, 1.0, 1.0}
let b = f32x4{1.0, 2.0, 3.0, 4.0}; // Per-element init
let c = a + b; // Element-wise addition
let x = c[0]; // Element access (float)
}
typedef float f32x4 ;
int
Arithmetic (+, -, *, /) and bitwise (&, |, ^) operators work element-wise. See std/simd.zc for predefined types.
Type Aliases
Create a new name for an existing type.
alias ID = int;
alias PointMap = Map<string, Point>
alias OpFunc = fn(int, int) -> int
typedef int ID;
typedef struct Map_string_Point PointMap;
typedef struct z_closure_T;
typedef z_closure_T OpFunc;
Opaque Type Aliases
You can define a type alias as opaque to create a new type that is distinct from its underlying type outside of the defining module. This provides strong encapsulation and type safety without the runtime overhead of a wrapper struct.
// In library.zc
opaque alias Handle = int;
fn make_handle(v: int) -> Handle {
return v; // Implicit conversion allowed inside module
}
// In main.zc
import "library.zc";
fn main() {
let h: Handle = make_handle(42);
// let i: int = h; // Error: Type validation failed
// let h2: Handle = 10; // Error: Type validation failed
}
// In library.h
typedef int Handle;
Handle ;
// In library.c
Handle
// In main.c
int