From d8ea7fbd70141baa01ee1de953c809268baee24c Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 22 Dec 2020 21:12:04 -0600 Subject: [PATCH] add reflection example --- reflection.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 reflection.zig diff --git a/reflection.zig b/reflection.zig new file mode 100644 index 0000000..30f0a20 --- /dev/null +++ b/reflection.zig @@ -0,0 +1,24 @@ +const std = @import("std"); + +const Header = struct { + magic: u32, + name: []const u8, +}; + +pub fn main() void { + printInfoAboutStruct(Header); +} + +fn printInfoAboutStruct(comptime T: type) void { + const info = @typeInfo(T); + inline for (info.Struct.fields) |field| { + std.debug.print( + "{} has field called {} with type {}\n", + .{ + @typeName(T), + field.name, + @typeName(field.field_type), + }, + ); + } +}