简体   繁体   English

zig(zig 语言)中的类型生成

[英]Generation of types in zig (zig language)

Is it possible to create a comptime function in zig that would generate a new struct type?是否可以在 zig 中创建一个可生成新结构类型的comptime function? The function would receive an array of strings and an array of types. function 将接收一个字符串数组和一个类型数组。 The strings are the names of subsequent struct fields.字符串是后续结构字段的名称。

This has been implemented now as https://github.com/ziglang/zig/pull/6099这现在已经实现为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 };
}

The TypeInfo struct is defined here . TypeInfo 结构在这里定义。

No. This has been long proposed at https://github.com/ziglang/zig/issues/383不,这已经在https://github.com/ziglang/zig/issues/383提出了很长时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM