diff options
| author | Patrick Schönberger | 2026-05-12 09:35:41 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2026-05-12 09:35:41 +0200 |
| commit | 80def4a3b95f25a4b7cd0e6d94f44a00cf1b4850 (patch) | |
| tree | 97dd0f55ed68e7ed985964e2ae2648b036bfbc13 | |
| parent | 13511389f63976147003dd0924ec3ca889aa280f (diff) | |
| download | reactive-zig-80def4a3b95f25a4b7cd0e6d94f44a00cf1b4850.tar.gz reactive-zig-80def4a3b95f25a4b7cd0e6d94f44a00cf1b4850.zip | |
make mutable
| -rw-r--r-- | src/refl.zig | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/refl.zig b/src/refl.zig index 3dd851f..8da4ff1 100644 --- a/src/refl.zig +++ b/src/refl.zig @@ -9,18 +9,18 @@ fn A(comptime T: type) type { inline for (ti.fields, 0..) |field, idx| { names[idx] = field.name; - types[idx] = extern struct { + types[idx] = struct { value: field.type, pub fn get(self: @This()) field.type { std.debug.print("getting {s}\n", .{field.name}); return self.value; } - pub fn set(self: @This(), value: field.type) void { + pub fn set(self: *@This(), value: field.type) void { std.debug.print("setting {s}\n", .{field.name}); self.value = value; } }; - const default = if (field.default_value_ptr) |def| types[idx] {.value = @as(*field.type, @constCast(@ptrCast(@alignCast(def)))).*} else null; + const default = if (field.default_value_ptr) |def| types[idx]{ .value = @as(*field.type, @ptrCast(@alignCast(@constCast(def)))).* } else null; attrs[idx] = .{ .@"comptime" = field.is_comptime, .@"align" = field.alignment, @@ -28,36 +28,41 @@ fn A(comptime T: type) type { }; } inline for (ti.decls, 0..) |decl, idx| { - const default = struct { - pub fn call() void { + names[ti.fields.len + idx] = decl.name; + const NT = struct { + pub fn call(self: @This()) void { + _ = self; std.debug.print("calling {s}\n", .{decl.name}); @call(.auto, @field(T, decl.name), .{}); std.debug.print("done calling {s}\n", .{decl.name}); } }; - names[ti.fields.len + idx] = decl.name; - types[ti.fields.len + idx] = type; - attrs[ti.fields.len + idx] = .{.default_value_ptr = &default}; + types[ti.fields.len + idx] = NT; + const default = NT {}; + attrs[ti.fields.len + idx] = .{ .default_value_ptr = &default }; } return @Struct(.auto, null, &names, &types, &attrs); } pub fn main() !void { - const a = A(struct { + var a = A(struct { i: i32 = 123, j: i32 = 456, + s: A(struct { + i: i32 = 789, + }) = .{}, pub fn b() void { std.debug.print("B!\n", .{}); } pub fn c() void { std.debug.print("C!\n", .{}); } - }) {}; - std.debug.print("[{}] {}\n", .{@TypeOf(a), a}); + }){}; + std.debug.print("[{}] {}\n", .{ @TypeOf(a), a }); a.b.call(); a.c.call(); std.debug.print("{}\n", .{a.i.get()}); + a.j.set(9); std.debug.print("{}\n", .{a.j.get()}); } - |
