在Zig中是否可以创建一个
comptime
函数,该函数将生成一个新的结构体类型?该函数将接收字符串数组和类型数组。 这些字符串是后续结构字段的名称。comptime
函数,该函数将生成一个新的结构体类型?该函数将接收字符串数组和类型数组。 这些字符串是后续结构字段的名称。现在已经实现了,具体请参见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 };
}
TypeInfo结构体的定义在这里被给出。