advanced/reflection #225
Replies: 2 comments 1 reply
-
其实 pub fn main() !void {
// if (@hasDecl(@This(), "foobar")) {} else @compileError("不存在这个函数");
if (@hasDecl(@This(), "qwq")) {} else @compileError("编译通过");
}
fn qwq() void {}
|
Beta Was this translation helpful? Give feedback.
1 reply
-
@typeinfo(comptime T: type) std.builtin.Type 返回值:返回一个结构体,描述 T 的完整类型信息(std.builtin.Type),这是一种 结构化的类型反射。 原因: @typeinfo 的目标是对类型本身进行编译期的结构性分析,比如你可能想用它来生成代码,或者在编译期做逻辑分支(如 switch (@typeinfo(T).Kind))。 所以它必须在 编译时完全知道类型的所有结构,Zig 才能生成相应的类型信息数据结构。 这也是为什么它需要参数 T 是 comptime 的。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
advanced/reflection
简单、快速地学习 Zig,ziglang中文教程,zig中文教程
https://course.ziglang.cc/advanced/reflection
Beta Was this translation helpful? Give feedback.
All reactions