this give me an error
error: type 'void' does not support struct initialization syntax
const ret: type = @Type(.{ .Type = .{ .Struct = .{
~^
pub fn frame(comptime _scema: type) !type {
switch (@typeInfo(_scema)) {
.Struct => |st| {
var Struc_fields: [st.fields.len]std.builtin.Type.StructField = undefined;
for (st.fields, 0..) |F, i| {
Struc_fields[i] = .{
.name = F.name,
.type = column(F.type),
.default_value = undefined,
.is_comptime = true,
.alignment = 4,
};
}
const ret: type = @Type(.{ .Type = .{ .Struct = .{
.fields = &Struc_fields,
.layout = .auto,
.is_tuple = false,
.decls = &.{},
} } });
return ret;
},
else => {
@compileError("Not a struct");
},
}
}
jameson-m-jolley:
.{ .Type =
This part is wrong. Type
is the type, not a field.
jmc
November 6, 2024, 6:43pm
3
Documentation for @Type()
: Documentation - The Zig Programming Language
In essence, you give it a type specification and it reifies (“materializes”) and returns that type for you to use. It takes a std.builtin.Type
, which is a tagged union, so you initialize its members by setting the active tag, and those have lowercase names. So, for example:
const ret = @Type(.{
.@"struct" = .{
// ...
},
};
1 Like
i am aware of what it does but this is the problem
error: use of undefined value here causes undefined behavior
const ret: type = @Type(.{ .Struct = .{
~^
Why is it telling me this causes undefined behavior? When I try to be more explicit, it gives me the error above.
what is the correct way of doing It?
you are giving @Type
a struct that contains undefined
entries:
.default_value = undefined,
You probably meant to use null
(denoting no default value) here.
From the documentation here is an explanation what undefined does:
Translated into English, undefined
means “Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used.”
dimdin
November 6, 2024, 7:09pm
8
For zig 0.13.0 Type use .Struct
(the dotted name of the field):
const ret: type = @Type(.Struct = .{
.fields = &Struc_fields,
In the latest zig (master/0.14.0) the name of the field is lower case struct
and .@"struct"
is used because struct
is a keyword.
the default value can’t be null if .comptime = true
this is the “working” solution
pub fn frame(comptime _scema: type) !type {
switch (@typeInfo(_scema)) {
.Struct => |st| {
var Struc_fields: [st.fields.len]std.builtin.Type.StructField = undefined;
for (st.fields, 0..) |F, i| {
Struc_fields[i] = .{
.name = F.name,
.type = column(F.type),
.default_value = null,
.is_comptime = false,
.alignment = 4,
};
}
const ret: type = @Type(.{ .Struct = .{
.fields = &Struc_fields,
.layout = .auto,
.is_tuple = false,
.decls = &.{},
} });
return ret;
},
else => {
@compileError("Not a struct");
},
}
}