Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const std = @import("std");
- const MyUnion = union(enum) {
- SingleInt: u8,
- String: [5]u8,
- };
- fn createInstance(tag: @TagType(MyUnion), data: var) MyUnion {
- var instance: MyUnion = undefined;
- // Is something like this possible?
- // instance[tag] = data
- return instance;
- }
- pub fn main() void {
- const single_int = createInstance(MyUnion.SingleInt, 42);
- std.debug.assert(std.meta.activeTag(single_int) == MyUnion.SingleInt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement