| ... | ... | @@ -2778,6 +2778,7 @@ test "simple union" { |
| 2778 | 2778 | This turns the union into a <em>tagged</em> union, which makes it eligible |
| 2779 | 2779 | to use with {#link|switch#} expressions. One can use {#link|@TagType#} to |
| 2780 | 2780 | obtain the enum type from the union type. |
| 2781 | Tagged unions implicitly cast to their enum {#link|Implicit Cast: unions and enums#} |
| 2781 | 2782 | </p> |
| 2782 | 2783 | {#code_begin|test#} |
| 2783 | 2784 | const std = @import("std"); |
| ... | ... | @@ -2805,6 +2806,14 @@ test "switch on tagged union" { |
| 2805 | 2806 | test "@TagType" { |
| 2806 | 2807 | assert(@TagType(ComplexType) == ComplexTypeTag); |
| 2807 | 2808 | } |
| 2809 | |
| 2810 | test "implicit cast to enum" { |
| 2811 | const c1 = ComplexType{ .Ok = 42 }; |
| 2812 | const c2 = ComplexType.NotOk; |
| 2813 | |
| 2814 | assert(c1 == .Ok); |
| 2815 | assert(c2 == .NotOk); |
| 2816 | } |
| 2808 | 2817 | {#code_end#} |
| 2809 | 2818 | <p>In order to modify the payload of a tagged union in a switch expression, |
| 2810 | 2819 | place a {#syntax#}*{#endsyntax#} before the variable name to make it a pointer: |