authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2025-08-17 18:35:12+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-17 19:17:02-07:00
logcf90a5e451fc170738966a0a1a06c2dd5cddc066
treed8a3faa37749d7059111aa6f4b47af897691fbbb
parentc315f2bc2eacc33e2a6e0fcb5b775e20aa506aa7

langref: add documentation for unions with inferred tag and explicit tag values


2 files changed, 24 insertions(+), 0 deletions(-)

doc/langref.html.in+7
...@@ -2469,6 +2469,13 @@ or...@@ -2469,6 +2469,13 @@ or
2469 </p>2469 </p>
2470 {#code|test_union_method.zig#}2470 {#code|test_union_method.zig#}
24712471
2472 <p>
2473 Unions with inferred enum tag types can also assign ordinal values to their inferred tag.
2474 This requires the tag to specify an explicit integer type.
2475 {#link|@intFromEnum#} can be used to access the ordinal value corresponding to the active field.
2476 </p>
2477 {#code|test_tagged_union_with_tag_values.zig#}
2478
2472 <p>2479 <p>
2473 {#link|@tagName#} can be used to return a {#link|comptime#}2480 {#link|@tagName#} can be used to return a {#link|comptime#}
2474 {#syntax#}[:0]const u8{#endsyntax#} value representing the field name:2481 {#syntax#}[:0]const u8{#endsyntax#} value representing the field name:
doc/langref/test_tagged_union_with_tag_values.zig created+17
...@@ -0,0 +1,17 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4const Tagged = union(enum(u32)) {
5 int: i64 = 123,
6 boolean: bool = 67,
7};
8
9test "tag values" {
10 const int: Tagged = .{ .int = -40 };
11 try expect(@intFromEnum(int) == 123);
12
13 const boolean: Tagged = .{ .boolean = false };
14 try expect(@intFromEnum(boolean) == 67);
15}
16
17// test