Skip to content

Instantly share code, notes, and snippets.

@winksaville
Created September 1, 2018 03:47
Show Gist options
  • Save winksaville/a094ac6fe99f7bccdf81301b2e942c0a to your computer and use it in GitHub Desktop.
Save winksaville/a094ac6fe99f7bccdf81301b2e942c0a to your computer and use it in GitHub Desktop.
How to make a struct in zig
//$ zig test make_struct.zig
// make_struct.zig:20:7: error: no member named 'int64' in struct 'makeStruct((struct []const u8 constant),i64)'
// v1.int64 = -123;
// Seems the "name" parameter needs to be a "symbol"
// and the return would be something like:
// return struct { @symbol(name): T, };
fn makeStruct(comptime name: []const u8, comptime T: type) type {
return struct { name: T, };
}
// Or maybe there is a "symbol" reserved word and then somthing like:
//fn makeStruct(comptime name: symbol, comptime T: type) type {
// return struct { name: T, };
//}
test "makeStruct" {
const U1 = comptime makeStruct("int64", i64);
var v1: U1 = undefined;
v1.int64 = -123;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment