是否可以comptime在 zig 中创建一个函数来生成新的结构类型?该函数将接收一个字符串数组和一个类型数组。字符串是后续结构字段的名称。
现在已实现为https://github.com/ziglang/zig/pull/6099
const builtin = @import("std").builtin;
const A = @Type(.{
.Struct = .{
.layout = .Auto,
.fields = &[_]builtin.TypeInfo.StructField{
.{ .name = "one", .field_type = i32, .default_value = null, .is_comptime = false, .alignment = 0 },
},
.decls = &[_]builtin.TypeInfo.Declaration{},
.is_tuple = false,
},
});
test "" {
const a: A = .{ .one = 25 };
}
Run Code Online (Sandbox Code Playgroud)
TypeInfo 结构在此处定义。