Skip to content

Instantly share code, notes, and snippets.

@softprops
Last active May 12, 2024 17:09
Show Gist options
  • Save softprops/92c1c09dfe0ca6869c2e583fc51fa3b4 to your computer and use it in GitHub Desktop.
Save softprops/92c1c09dfe0ca6869c2e583fc51fa3b4 to your computer and use it in GitHub Desktop.
const std = @import("std");
const meta = std.meta;
const Example = struct {
foo: []const u8,
bar: i32,
const docs = std.EnumArray(
meta.FieldEnum(Example),
// πŸ‘‡ type of value of metadata checked at comptime, can be any type declared
[]const u8,
).init(
.{
.foo = "foo will do a thing",
.bar = "bar will do this other thing",
//πŸ‘‡ will fail at comptime if struct field is omitted here declared here but not in struct
//.otherfield
},
);
};
pub fn main() void {
std.debug.print(
"{s}",
//πŸ‘‡ will fail at comptime if passed invalid field ref
.{Example.docs.get(.baz)},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment