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" {...@@ -3824,7 +3824,7 @@ test "simple union" {
3824 to use with {#link|switch#} expressions.3824 to use with {#link|switch#} expressions.
3825 Tagged unions coerce to their tag type: {#link|Type Coercion: unions and enums#}.3825 Tagged unions coerce to their tag type: {#link|Type Coercion: unions and enums#}.
3826 </p>3826 </p>
3827 {#code_begin|test|test_switch_tagged_union#}3827 {#code_begin|test|test_tagged_union#}
3828const std = @import("std");3828const std = @import("std");
3829const expect = std.testing.expect;3829const expect = std.testing.expect;
38303830
...@@ -3850,14 +3850,6 @@ test "switch on tagged union" {...@@ -3850,14 +3850,6 @@ test "switch on tagged union" {
3850test "get tag type" {3850test "get tag type" {
3851 try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);3851 try expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
3852}3852}
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}
3861 {#code_end#}3853 {#code_end#}
3862 <p>In order to modify the payload of a tagged union in a switch expression,3854 <p>In order to modify the payload of a tagged union in a switch expression,
3863 place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:3855 place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer:
...@@ -3877,7 +3869,6 @@ const ComplexType = union(ComplexTypeTag) {...@@ -3877,7 +3869,6 @@ const ComplexType = union(ComplexTypeTag) {
38773869
3878test "modify tagged union in switch" {3870test "modify tagged union in switch" {
3879 var c = ComplexType{ .ok = 42 };3871 var c = ComplexType{ .ok = 42 };
3880 try expect(@as(ComplexTypeTag, c) == ComplexTypeTag.ok);
38813872
3882 switch (c) {3873 switch (c) {
3883 ComplexTypeTag.ok => |*value| value.* += 1,3874 ComplexTypeTag.ok => |*value| value.* += 1,