From 5af7b9fc34996094674e5ca1102b14cc0d4c5317 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Mon, 1 Jan 2024 18:44:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8F=8D=E5=B0=84=E7=AB=A0?= =?UTF-8?q?=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- learn/more/reflection.md | 45 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/learn/more/reflection.md b/learn/more/reflection.md index e6f519b8..8d4bf0f5 100644 --- a/learn/more/reflection.md +++ b/learn/more/reflection.md @@ -263,4 +263,47 @@ zig 除了获取类型信息外,还提供了在编译期构建全新类型的 该函数实际上就是 `@typeInfo` 的反函数,它将类型信息具体化为一个类型。 -TODO +函数的原型为: + +`@Type(comptime info: std.builtin.Type) type` + +参数的具体类型可以参考 [此处](https://ziglang.org/documentation/master/std/#A;std:builtin.Type)。 + +以下示例为我们构建一个新的结构体: + +```zig +const std = @import("std"); + +const T = @Type(.{ + .Struct = .{ + .layout = .Auto, + .fields = &.{ + .{ + .alignment = 8, + .name = "b", + .type = u32, + .is_comptime = false, + .default_value = null, + }, + }, + .decls = &.{}, + .is_tuple = false, + }, +}); + +pub fn main() !void { + const D = T{ + .b = 666, + }; + + std.debug.print("{}\n", .{D.b}); +} +``` + +::: warning + +需要注意的是,当前 zig 并不支持构建的类型包含声明(declaration),即定义的变量(常量)或方法,具体原因见此 [issue](https://github.com/ziglang/zig/issues/6709)! + +不得不说,不支持声明极大地降低了 zig 编译期的特性。 + +:::