mcpField
Z
zig-comptime

zig-comptime

github.com/mohitmishra786开发269453 次安装18 周前更新
代码仓库

关于 zig-comptime

zig-comptime 是面向 Zig 开发者的 comptime 指导技能,讲解编译期求值、泛型和类型反射,并展示用 comptime 生成代码的模式。

主要功能

  1. 01使用 comptime 编译期求值
  2. 02通过类型参数编写泛型函数
  3. 03用 anytype 推断参数类型
  4. 04通过 @typeInfo 反射类型
  5. 05在编译期生成代码

适用场景

  1. 01为多种类型编写通用函数
  2. 02检查并处理类型结构
  3. 03编译期生成查找表
  4. 04用编译期逻辑替代部分宏

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill zig-comptime

安装后在 Claude Code 中输入/zig-comptime确认已加载。

详细介绍

Zig comptime

用途

指导 Agent 使用 Zig 的 comptime 系统:编译期函数求值、comptime 类型参数、通过 anytype 实现泛型、使用 @typeInfo 进行类型反射,以及替代 C++ 模板和宏的元编程模式。

触发条件

  • “Zig 中的 comptime 是如何工作的?”
  • “如何在 Zig 中编写泛型函数?”
  • “如何使用 @typeInfo 进行反射?”
  • “如何在 Zig 中于编译期生成代码?”
  • “anytype 在 Zig 中如何工作?”
  • “Zig 泛型与 C++ 模板有什么区别?”

工作流

1. comptime 基础
// comptime keyword forces compile-time evaluation
const x: comptime_int = 42;         // comptime integer (arbitrary precision)
const y: comptime_float = 3.14159;  // comptime float (arbitrary precision)

// comptime block — runs at compile time
comptime {
    const val = fibonacci(20);       // computed at compile time
    std.debug.assert(val == 6765);   // compile-time assertion
}

// comptime parameter — caller must provide a comptime-known value
fn makeArray(comptime T: type, comptime n: usize) [n]T {
    return [_]T{0} ** n;             // array of n zeros of type T
}

const arr = makeArray(f32, 8);      // [8]f32 computed at compile time
2. 使用 comptime 类型参数编写泛型函数
const std = @import("std");

// Generic max function — T must be comptime-known
fn max(comptime T: type, a: T, b: T) T {
    return if (a > b) a else b;
}

// Usage: T is inferred from arguments or specified explicitly
const r1 = max(i32, 3, 7);          // 7
const r2 = max(f64, 2.5, 1.8);     // 2.5

// Generic Stack data structure
fn Stack(comptime T: type) type {
    return struct {
        items: []T,
        top: usize,
        allocator: std.mem.Allocator,

        const Self = @This();

        pub fn init(allocator: std.mem.Allocator) !Self {
            return Self{
                .items = try allocator.alloc(T, 64),
                .top = 0,
                .allocator = allocator,
            };
        }

        pub fn push(self: *Self, value: T) void {
            self.items[self.top] = value;
            self.top += 1;
        }

        pub fn pop(self: *Self) ?T {
            if (self.top == 0) return null;
            self.top -= 1;
            return self.items[self.top];
        }

        pub fn deinit(self: *Self) void {
            self.allocator.free(self.items);
        }
    };
}

// Usage: Stack(i32) and Stack(f64) are distinct types
var int_stack = try Stack(i32).init(allocator);
defer int_stack.deinit();
int_stack.push(42);
3. anytype——鸭子类型的 comptime 参数

anytype 接受任意类型,编译器会在调用处推断类型:

// anytype: function works for any type with .len field
fn printLength(thing: anytype) void {
    std.debug.print("Length: {}\n", .{thing.len});
}

printLength("hello");                // string literal — works
printLength([_]u8{1, 2, 3});        // array — works
printLength(std.ArrayList(u32){});   // ArrayList — works

// anytype with comptime checks for better errors
fn serialize(writer: anytype, value: anytype) !void {
    // Verify writer has write method at comptime
    if (!@hasDecl(@TypeOf(writer), "write")) {
        @compileError("writer must have a write method");
    }
    try writer.write(std.mem.asBytes(&value));
}

// anytype in struct methods (used throughout std library)
pub fn format(
    self: MyType,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,    // any writer: file, buffer, etc.
) !void {
    try writer.print("{} {}", .{self.x, self.y});
}
4. 使用 @typeInfo 进行类型反射

@typeInfo 会在 comptime 阶段返回一个描述类型结构的带标签联合:

const std = @import("std");
const TypeInfo = std.builtin.Type;

fn printTypeInfo(comptime T: type) void {
    const info = @typeInfo(T);
    switch (info) {
        .Int => |i| std.debug.print("Int: {} bits, {s}\n",
            .{i.bits, @tagName(i.signedness)}),
        .Float => |f| std.debug.print("Float: {} bits\n", .{f.bits}),
        .Struct => |s| {
            std.debug.print("Struct with {} fields:\n", .{s.fields.len});
            inline for (s.fields) |field| {
                std.debug.print("  {s}: {}\n", .{field.name, field.type});
            }
        },
        .Enum => |e| {
            std.debug.print("Enum with {} values:\n", .{e.fields.len});
            inline for (e.fields) |field| {
                std.debug.print("  {s} = {}\n", .{field.name, field.value});
            }
        },
        .Optional => |o| std.debug.print("Optional({s})\n", .{@typeName(o.child)}),
        .Array => |a| std.debug.print("[{}]{s}\n", .{a.len, @typeName(a.child)}),
        else => std.debug.print("Other type: {s}\n", .{@typeName(T)}),
    }
}

// Usage at comptime
comptime { printTypeInfo(u32); }   // Int: 32 bits, unsigned
comptime { printTypeInfo(f64); }   // Float: 64 bits
5. comptime 生成代码的模式
// Generate a lookup table at comptime
const sin_table = blk: {
    const N = 256;
    var table: [N]f32 = undefined;
    @setEvalBranchQuota(10000);     // increase for expensive comptime eval
    for (0..N) |i| {
        const angle = @as(f32, @floatFromInt(i)) * (2.0 * std.math.pi / N);
        table[i] = @sin(angle);
    }
    break :blk table;
};

// Comptime string processing
fn upperCase(comptime s: []const u8) [s.len]u8 {
    var result: [s.len]u8 = undefined;
    for (s, 0..) |c, i| {
        result[i] = std.ascii.toUpper(c);
    }
    return result;
}
const HELLO = upperCase("hello");  // computed at compile time

// Structural typing: accept any struct with specific fields
fn area(shape: anytype) f64 {
    const T = @TypeOf(shape);
    if (@hasField(T, "width") and @hasField(T, "height")) {
        return @as(f64, shape.width) * @as(f64, shape.height);
    } else if (@hasField(T, "radius")) {
        return std.math.pi * @as(f64, shape.radius) * @as(f64, shape.radius);
    } else {
        @compileError("shape must have width+height or radius fields");
    }
}
6. comptime 与 C++ 模板对比
特性C++ 模板Zig comptime
语法template<typename T>fn foo(comptime T: type)
错误消息难以理解的实例化调用栈清晰,出现在定义处
特化template<> class Foo<int>使用 inline if 的 if (T == i32) { ... }
SFINAE复杂的 enable_if@hasDecl、@hasField、@compileError
可变参数template<typename... Ts>anytype、元组、inline for
编译时间可能非常慢显式,由 @setEvalBranchQuota 限定
值需要 constexpr任意表达式都可以是 comptime
宏独立的 #define 系统comptime 函数可替代大多数宏
7. 常见 comptime 模式
// Pattern: compile error for unsupported types
fn serializeInt(comptime T: type, value: T) []const u8 {
    if (@typeInfo(T) != .Int) {
        @compileError("serializeInt requires an integer type, got: " ++ @typeName(T));
    }
    // ...
}

// Pattern: conditional compilation
const is_debug = @import("builtin").mode == .Debug;
if (comptime is_debug) {
    // included only in debug builds
    validateInvariant(self);
}

// Pattern: inline for over comptime-known slice
const fields = std.meta.fields(MyStruct);
inline for (fields) |field| {
    // field.name, field.type available at comptime
    std.debug.print("{s}\n", .{field.name});
}

相关技能

  • 使用 skills/zig/zig-testing 编写 comptime 断言并测试 comptime 代码
  • 使用 skills/zig/zig-build-system 配置基于 comptime 的 build.zig
  • 使用 skills/compilers/cpp-templates 了解 C++ 模板的等效模式

评论

支持 Markdown