authorgravatar for yujiri@disroot.orgEvin Yulo <yujiri@disroot.org> 2022-12-11 12:00:31+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-13 15:11:43-05:00
log02b4ea71e3a30d9076cd0c145777c792f2e4245b
tree3811def50f0de121e3cb8cad9d1c0965f0869c43
parent35c6fe665c7f3f8ba14aaee97c832000cbf5ddc2

Improve tagged union documentation

closes #13870

1 files changed, 1 insertions(+), 10 deletions(-)

doc/langref.html.in+1-10
......@@ -3824,7 +3824,7 @@ test "simple union" {
38243824 to use with {#link|switch#} expressions.
38253825 Tagged unions coerce to their tag type: {#link|Type Coercion: unions and enums#}.
38263826 </p>
3827 {#code_begin|test|test_switch_tagged_union#}
3827 {#code_begin|test|test_tagged_union#}
38283828const std = @import("std");
38293829const expect = std.testing.expect;
38303830
......@@ -3850,14 +3850,6 @@ test "switch on tagged union" {
38503850test "get tag type" {
38513851 try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
38523852}
3853
3854test "coerce to enum" {
3855 const c1 = ComplexType{ .ok = 42 };
3856 const c2 = ComplexType.not_ok;
3857
3858 try expect(c1 == .ok);
3859 try expect(c2 == .not_ok);
3860}
38613853 {#code_end#}
38623854 <p>In order to modify the payload of a tagged union in a switch expression,
38633855 place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:
......@@ -3877,7 +3869,6 @@ const ComplexType = union(ComplexTypeTag) {
38773869
38783870test "modify tagged union in switch" {
38793871 var c = ComplexType{ .ok = 42 };
3880 try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
38813872
38823873 switch (c) {
38833874 ComplexTypeTag.ok => |*value| value.* += 1,