From 8960e8090e2c5a5f491e09baf8df65f52d0daf77 Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Sun, 27 Oct 2019 21:35:22 +0100 Subject: [PATCH] make implicit cast of tagged unions to enums easier to find in docs --- doc/langref.html.in | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 618ea08cdb..3eb30c3058 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2778,6 +2778,7 @@ test "simple union" { This turns the union into a tagged union, which makes it eligible to use with {#link|switch#} expressions. One can use {#link|@TagType#} to obtain the enum type from the union type. + Tagged unions implicitly cast to their enum {#link|Implicit Cast: unions and enums#}
{#code_begin|test#} const std = @import("std"); @@ -2805,6 +2806,14 @@ test "switch on tagged union" { test "@TagType" { assert(@TagType(ComplexType) == ComplexTypeTag); } + +test "implicit cast to enum" { + const c1 = ComplexType{ .Ok = 42 }; + const c2 = ComplexType.NotOk; + + assert(c1 == .Ok); + assert(c2 == .NotOk); +} {#code_end#}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: