diff --git a/doc/langref.html.in b/doc/langref.html.in index 8324b2eb98..1a8e3827f4 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2469,6 +2469,13 @@ or
{#code|test_union_method.zig#} ++ 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. +
+ {#code|test_tagged_union_with_tag_values.zig#} +{#link|@tagName#} can be used to return a {#link|comptime#} {#syntax#}[:0]const u8{#endsyntax#} value representing the field name: diff --git a/doc/langref/test_tagged_union_with_tag_values.zig b/doc/langref/test_tagged_union_with_tag_values.zig new file mode 100644 index 0000000000..c079b5eac3 --- /dev/null +++ b/doc/langref/test_tagged_union_with_tag_values.zig @@ -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