-
Notifications
You must be signed in to change notification settings - Fork 49
advanced/reflection #225
advanced/reflection
#225
-
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
其实 @hasDecl 还可用于判断 函数 是否存在。
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.
All reactions
1 reply
-
是的,可以判断当前容器内的函数是否存在,但是如果是其他的容器,则只能探查 pub 修饰的函数
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
@typeinfo(comptime T: type) std.builtin.Type
参数要求:T 必须是 编译期已知类型,即 comptime。
返回值:返回一个结构体,描述 T 的完整类型信息(std.builtin.Type),这是一种 结构化的类型反射。
原因:
@typeinfo 的目标是对类型本身进行编译期的结构性分析,比如你可能想用它来生成代码,或者在编译期做逻辑分支(如 switch (@typeinfo(T).Kind))。
所以它必须在 编译时完全知道类型的所有结构,Zig 才能生成相应的类型信息数据结构。
这也是为什么它需要参数 T 是 comptime 的。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment