Hi all,
in the process of learning Zig, I’m trying to port a little C project that involves reflection. In C I had to do a lot of macro trickery and I did not enjoy the experience, so I thought this was a good opportunity for Zig.
I’m currently stuck and I don’t have the slightest idea of what I’m doing wrong. The error happens at runtime during schema.dump
. I have a memory corruption issue while printing the second field.
const std = @import("std");
const stdout = std.io.getStdOut().writer();
const Field = struct {
name: []const u8,
description: []const u8 = "No description provided",
//default: ?Variant = null,
};
const Schema = struct {
filename: []const u8,
fields: []const Field,
pub fn factory(filename: []const u8, comptime settings: anytype) Schema {
const fields = init: {
var rows: [settings.len]Field = undefined;
inline for (&rows, settings) |*row, setting| {
row.* = Field{ .name = setting[0], .description = setting[1] };
}
break :init rows;
};
// This works fine:
//const fields = .{
// Field{ .name = settings[0][0], .description = settings[0][1] },
// Field{ .name = settings[1][0], .description = settings[1][1] },
// Field{ .name = settings[2][0], .description = settings[2][1] },
// Field{ .name = settings[3][0], .description = settings[3][1] },
// Field{ .name = settings[4][0], .description = settings[4][1] },
//};
return Schema{
.filename = filename,
.fields = &fields,
};
}
fn dump(self: Schema, writer: anytype) !void {
for (self.fields) |field| {
try writer.print(".{{ \"{[name]s}\", \"{[description]s}\" }},\n", field);
}
}
};
pub fn main() !void {
// `settings` is always comptime known
const settings = .{
.{ "setting1", "A boolean", bool, true },
.{ "setting2", "An int", i32, -123 },
.{ "setting3", "An unsigned", u32, 123 },
.{ "setting4", "A float", f64, -1.23 },
.{ "setting5", "A string", [100]u8, "abc" },
};
const schema = Schema.factory("testfile", settings);
try schema.dump(stdout);
}