Skip to content

Instantly share code, notes, and snippets.

@schmee
Created March 12, 2019 20:15
Show Gist options
  • Save schmee/28a986e753b46ae8b161df7f71c17f90 to your computer and use it in GitHub Desktop.
Save schmee/28a986e753b46ae8b161df7f71c17f90 to your computer and use it in GitHub Desktop.
inferred union cast
pub const Leaf = struct {
frequency: u32,
value: u8,
};
pub const Branch = struct {
frequency: u32,
left: ?*Node,
right: ?*Node,
};
pub const Node = union(enum) {
Branch: Branch,
Leaf: Leaf,
};
pub fn main() !void {
const node: Node = Node {
.Leaf = Leaf {
.frequency = 1,
.value = 1,
}
};
const Tag: @TagType(Node) = node;
const is_branch = node == Tag.Branch;
}
➜ git:(master) ✗ zig build-exe asdf.zig && ./asdf
asdf.zig:25:34: error: no member named 'Branch' in enum '@TagType(Node)'
const is_branch = node == Tag.Branch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment