- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
One of Zig’s most powerful features is its ability to run code at compile time. This enables code generation, validation, and optimizations that would be much harder (or impossible) in many other languages. In this tutorial, we’ll walk through practical examples of Zig's compile-time programming and how you can use it effectively.
Step 1: Basic Compile-Time Execution
You can run code at compile time using comptime:
const std = @import("std");
const num = comptime calculate();
fn calculate() usize {
std.debug.print("Evaluating at compile time\n", .{});
return 42;
}
Here, calculate() is run at compile time, and num becomes a constant value.
Step 2: Generics Using comptime Parameters
Zig lets you write generic functions and types using comptime parameters:
fn identity(comptime T: type, value: T) T {
return value;
}
const result = identity(i32, 123);
This function works with any type — Zig generates the function per type at compile time.
Step 3: Compile-Time Reflection
You can inspect types using @typeInfo:
fn printFields(comptime T: type) void {
const info = @typeInfo(T);
if (info.* == .Struct) {
for (info.Struct.fields) |field| {
std.debug.print("Field: {s}\n", .{field.name});
}
}
}
const MyStruct = struct {
x: i32,
y: f32,
};
pub fn main() void {
printFields(MyStruct);
}
This prints the field names of MyStruct — all at compile time.
Step 4: Creating Compile-Time Data Tables
You can even generate lookup tables at compile time:
const fib = comptime buildFibTable(10);
fn buildFibTable(n: usize) [10]usize {
var table: [10]usize = undefined;
table[0] = 0;
table[1] = 1;
for (2..n) |i| {
table = table[i - 1] + table[i - 2];
}
return table;
}
fib is a static array with the first 10 Fibonacci numbers, generated before runtime.
Step 5: Compile-Time Validation
You can enforce invariants early:
fn validateString(comptime s: []const u8) void {
if (s.len > 10) {
@compileError("String is too long!");
}
}
const _ = validateString("short"); // OK
// const _ = validateString("this is way too long"); // Compile error
Pros and
Cons of Compile-Time Programming in Zig
Pros:
Cons:
Compile-time programming in Zig is a powerful tool, giving you the ability to shape your programs with precision and safety. Whether you’re generating code, validating structures, or writing generic utilities, comptime allows you to bring logic forward in time — making your runtime simpler and your errors easier to catch.
If this was helpful, you can also support me here:
Step 1: Basic Compile-Time Execution
You can run code at compile time using comptime:
const std = @import("std");
const num = comptime calculate();
fn calculate() usize {
std.debug.print("Evaluating at compile time\n", .{});
return 42;
}
Here, calculate() is run at compile time, and num becomes a constant value.
Step 2: Generics Using comptime Parameters
Zig lets you write generic functions and types using comptime parameters:
fn identity(comptime T: type, value: T) T {
return value;
}
const result = identity(i32, 123);
This function works with any type — Zig generates the function per type at compile time.
Step 3: Compile-Time Reflection
You can inspect types using @typeInfo:
fn printFields(comptime T: type) void {
const info = @typeInfo(T);
if (info.* == .Struct) {
for (info.Struct.fields) |field| {
std.debug.print("Field: {s}\n", .{field.name});
}
}
}
const MyStruct = struct {
x: i32,
y: f32,
};
pub fn main() void {
printFields(MyStruct);
}
This prints the field names of MyStruct — all at compile time.
Step 4: Creating Compile-Time Data Tables
You can even generate lookup tables at compile time:
const fib = comptime buildFibTable(10);
fn buildFibTable(n: usize) [10]usize {
var table: [10]usize = undefined;
table[0] = 0;
table[1] = 1;
for (2..n) |i| {
table = table[i - 1] + table[i - 2];
}
return table;
}
fib is a static array with the first 10 Fibonacci numbers, generated before runtime.
Step 5: Compile-Time Validation
You can enforce invariants early:
fn validateString(comptime s: []const u8) void {
if (s.len > 10) {
@compileError("String is too long!");
}
}
const _ = validateString("short"); // OK
// const _ = validateString("this is way too long"); // Compile error
Fast, efficient programs with zero runtime overhead- ? Expressive generics and metaprogramming
- ? Early validation for safer code
- ? Replace macro systems with actual code
- ? Harder to debug or trace than runtime logic
- ? Steep learning curve for beginners
- ? Can become overly abstract if misused
Compile-time programming in Zig is a powerful tool, giving you the ability to shape your programs with precision and safety. Whether you’re generating code, validating structures, or writing generic utilities, comptime allows you to bring logic forward in time — making your runtime simpler and your errors easier to catch.
If this was helpful, you can also support me here: