| ... | @@ -2461,7 +2461,8 @@ or | ... | @@ -2461,7 +2461,8 @@ or |
| 2461 | {#header_open|Tagged union#} | 2461 | {#header_open|Tagged union#} |
| 2462 | <p>Unions can be declared with an enum tag type. | 2462 | <p>Unions can be declared with an enum tag type. |
| 2463 | This turns the union into a <em>tagged</em> union, which makes it eligible | 2463 | This turns the union into a <em>tagged</em> union, which makes it eligible |
| 2464 | to use with {#link|switch#} expressions. | 2464 | to use with {#link|switch#} expressions. When switching on tagged unions, |
| | 2465 | the tag value can be obtained using an additional capture. |
| 2465 | Tagged unions coerce to their tag type: {#link|Type Coercion: Unions and Enums#}. | 2466 | Tagged unions coerce to their tag type: {#link|Type Coercion: Unions and Enums#}. |
| 2466 | </p> | 2467 | </p> |
| 2467 | {#code|test_tagged_union.zig#} | 2468 | {#code|test_tagged_union.zig#} |
| ... | @@ -2594,6 +2595,13 @@ or | ... | @@ -2594,6 +2595,13 @@ or |
| 2594 | | 2595 | |
| 2595 | {#header_close#} | 2596 | {#header_close#} |
| 2596 | | 2597 | |
| | 2598 | {#header_open|Switching on errors#} |
| | 2599 | <p> |
| | 2600 | When switching on errors, some special cases are allowed to simplify generic programming patterns: |
| | 2601 | </p> |
| | 2602 | {#code|test_switch_on_errors.zig#} |
| | 2603 | {#header_close#} |
| | 2604 | |
| 2597 | {#header_open|Labeled switch#} | 2605 | {#header_open|Labeled switch#} |
| 2598 | <p> | 2606 | <p> |
| 2599 | When a switch statement is labeled, it can be referenced from a | 2607 | When a switch statement is labeled, it can be referenced from a |
| ... | @@ -2659,12 +2667,13 @@ or | ... | @@ -2659,12 +2667,13 @@ or |
| 2659 | {#code|test_inline_else.zig#} | 2667 | {#code|test_inline_else.zig#} |
| 2660 | | 2668 | |
| 2661 | <p> | 2669 | <p> |
| 2662 | When using an inline prong switching on an union an additional | 2670 | When using an inline prong switching on an union an additional capture |
| 2663 | capture can be used to obtain the union's enum tag value. | 2671 | can be used to obtain the union's enum tag value at comptime, even though |
| | 2672 | its payload might only be known at runtime. |
| 2664 | </p> | 2673 | </p> |
| 2665 | {#code|test_inline_switch_union_tag.zig#} | 2674 | {#code|test_inline_switch_union_tag.zig#} |
| 2666 | | 2675 | |
| 2667 | {#see_also|inline while|inline for#} | 2676 | {#see_also|inline while|inline for|Tagged union#} |
| 2668 | {#header_close#} | 2677 | {#header_close#} |
| 2669 | {#header_close#} | 2678 | {#header_close#} |
| 2670 | | 2679 | |