| author | |
| committer | |
| log | cf90a5e451fc170738966a0a1a06c2dd5cddc066 |
| tree | d8a3faa37749d7059111aa6f4b47af897691fbbb |
| parent | c315f2bc2eacc33e2a6e0fcb5b775e20aa506aa7 |
2 files changed, 24 insertions(+), 0 deletions(-)
doc/langref.html.in+7| ... | ... | @@ -2469,6 +2469,13 @@ or |
| 2469 | 2469 | </p> |
| 2470 | 2470 | {#code|test_union_method.zig#} |
| 2471 | 2471 | |
| 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 | 2479 | <p> |
| 2473 | 2480 | {#link|@tagName#} can be used to return a {#link|comptime#} |
| 2474 | 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 @@ |
| 1 | const std = @import("std"); | |
| 2 | const expect = std.testing.expect; | |
| 3 | ||
| 4 | const Tagged = union(enum(u32)) { | |
| 5 | int: i64 = 123, | |
| 6 | boolean: bool = 67, | |
| 7 | }; | |
| 8 | ||
| 9 | test "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 |