Improve tagged union documentation

closes #13870
This commit is contained in:
Evin Yulo 2022-12-11 12:00:31 +00:00 committed by Andrew Kelley
parent 35c6fe665c
commit 02b4ea71e3

View File

@ -3824,7 +3824,7 @@ test "simple union" {
to use with {#link|switch#} expressions.
Tagged unions coerce to their tag type: {#link|Type Coercion: unions and enums#}.
</p>
{#code_begin|test|test_switch_tagged_union#}
{#code_begin|test|test_tagged_union#}
const std = @import("std");
const expect = std.testing.expect;
@ -3850,14 +3850,6 @@ test "switch on tagged union" {
test "get tag type" {
try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
}
test "coerce to enum" {
const c1 = ComplexType{ .ok = 42 };
const c2 = ComplexType.not_ok;
try expect(c1 == .ok);
try expect(c2 == .not_ok);
}
{#code_end#}
<p>In order to modify the payload of a tagged union in a switch expression,
place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:
@ -3877,7 +3869,6 @@ const ComplexType = union(ComplexTypeTag) {
test "modify tagged union in switch" {
var c = ComplexType{ .ok = 42 };
try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
switch (c) {
ComplexTypeTag.ok => |*value| value.* += 1,