Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,13 @@ or
</p>
{#code|test_union_method.zig#}

<p>
Unions with inferred enum tag types can also assign ordinal values to their inferred tag.
This requires the tag to specify an explicit integer type.
{#link|@intFromEnum#} can be used to access the ordinal value corresponding to the active field.
</p>
{#code|test_tagged_union_with_tag_values.zig#}

<p>
{#link|@tagName#} can be used to return a {#link|comptime#}
{#syntax#}[:0]const u8{#endsyntax#} value representing the field name:
Expand Down
17 changes: 17 additions & 0 deletions doc/langref/test_tagged_union_with_tag_values.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const std = @import("std");
const expect = std.testing.expect;

const Tagged = union(enum(u32)) {
int: i64 = 123,
boolean: bool = 67,
};

test "tag values" {
const int: Tagged = .{ .int = -40 };
try expect(@intFromEnum(int) == 123);

const boolean: Tagged = .{ .boolean = false };
try expect(@intFromEnum(boolean) == 67);
}

// test