Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. const std = @import("std");
  2.  
  3. const MyUnion = union(enum) {
  4.     SingleInt: u8,
  5.     String: [5]u8,
  6. };
  7.  
  8. fn createInstance(tag: @TagType(MyUnion), data: var) MyUnion {
  9.     var instance: MyUnion = undefined;
  10.  
  11.     // Is something like this possible?
  12.     // instance[tag] = data
  13.    
  14.     return instance;
  15. }
  16.  
  17. pub fn main() void {
  18.     const single_int = createInstance(MyUnion.SingleInt, 42);
  19.  
  20.     std.debug.assert(std.meta.activeTag(single_int) == MyUnion.SingleInt);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement