diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/refl.zig | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/refl.zig b/src/refl.zig index 8da4ff1..c26225e 100644 --- a/src/refl.zig +++ b/src/refl.zig @@ -1,6 +1,16 @@ const std = @import("std"); -fn A(comptime T: type) type { +fn Args(params: []const std.builtin.Type.Fn.Param) type { + var argTypes: [params.len]type = undefined; + + for (params, 0..) |p, i| { + argTypes[i] = p.type.?; + } + + return @Tuple(&argTypes); +} + +fn Val(comptime T: type) type { const ti = @typeInfo(T).@"struct"; var names: [ti.fields.len + ti.decls.len][]const u8 = undefined; @@ -29,11 +39,13 @@ fn A(comptime T: type) type { } inline for (ti.decls, 0..) |decl, idx| { names[ti.fields.len + idx] = decl.name; + const fi = @typeInfo(@TypeOf(@field(T, decl.name))).@"fn"; + const NT = struct { - pub fn call(self: @This()) void { + pub fn call(self: @This(), args: Args(fi.params)) (fi.return_type orelse void) { _ = self; std.debug.print("calling {s}\n", .{decl.name}); - @call(.auto, @field(T, decl.name), .{}); + @call(.auto, @field(T, decl.name), args); std.debug.print("done calling {s}\n", .{decl.name}); } }; @@ -46,22 +58,27 @@ fn A(comptime T: type) type { } pub fn main() !void { - var a = A(struct { + var a = Val(struct { i: i32 = 123, j: i32 = 456, - s: A(struct { + s: Val(struct { i: i32 = 789, }) = .{}, pub fn b() void { std.debug.print("B!\n", .{}); } - pub fn c() void { - std.debug.print("C!\n", .{}); + pub fn c(self: @This()) void { + std.debug.print("C! ({})\n", .{self}); + } + pub fn d(self: @This(), i: i32) void { + self.i += i; + std.debug.print("D! ({})\n", .{self}); } }){}; std.debug.print("[{}] {}\n", .{ @TypeOf(a), a }); - a.b.call(); - a.c.call(); + a.b.call(.{}); + a.c.call(.{a}); + a.c.call(.{a, 5}); std.debug.print("{}\n", .{a.i.get()}); a.j.set(9); std.debug.print("{}\n", .{a.j.get()}); |
