| ... | ... | @@ -2350,12 +2350,12 @@ fn doTheTest() void { |
| 2350 | 2350 | var full = Full{ .number = 0x1234 }; |
| 2351 | 2351 | var divided = @bitCast(Divided, full); |
| 2352 | 2352 | switch (builtin.endian) { |
| 2353 | | builtin.Endian.Big => { |
| 2353 | .Big => { |
| 2354 | 2354 | assert(divided.half1 == 0x12); |
| 2355 | 2355 | assert(divided.quarter3 == 0x3); |
| 2356 | 2356 | assert(divided.quarter4 == 0x4); |
| 2357 | 2357 | }, |
| 2358 | | builtin.Endian.Little => { |
| 2358 | .Little => { |
| 2359 | 2359 | assert(divided.half1 == 0x34); |
| 2360 | 2360 | assert(divided.quarter3 == 0x2); |
| 2361 | 2361 | assert(divided.quarter4 == 0x1); |
| ... | ... | @@ -2630,6 +2630,8 @@ test "@tagName" { |
| 2630 | 2630 | assert(mem.eql(u8, @tagName(Small.Three), "Three")); |
| 2631 | 2631 | } |
| 2632 | 2632 | {#code_end#} |
| 2633 | {#see_also|@memberName|@memberCount|@tagName|@sizeOf#} |
| 2634 | |
| 2633 | 2635 | {#header_open|extern enum#} |
| 2634 | 2636 | <p> |
| 2635 | 2637 | By default, enums are not guaranteed to be compatible with the C ABI: |
| ... | ... | @@ -2646,6 +2648,7 @@ const Foo = extern enum { A, B, C }; |
| 2646 | 2648 | export fn entry(foo: Foo) void { } |
| 2647 | 2649 | {#code_end#} |
| 2648 | 2650 | {#header_close#} |
| 2651 | |
| 2649 | 2652 | {#header_open|packed enum#} |
| 2650 | 2653 | <p>By default, the size of enums is not guaranteed.</p> |
| 2651 | 2654 | <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the |
| ... | ... | @@ -2664,8 +2667,40 @@ test "packed enum" { |
| 2664 | 2667 | {#code_end#} |
| 2665 | 2668 | <p>This makes the enum eligible to be in a {#link|packed struct#}.</p> |
| 2666 | 2669 | {#header_close#} |
| 2667 | | {#see_also|@memberName|@memberCount|@tagName|@sizeOf#} |
| 2670 | |
| 2671 | {#header_open|Enum Literals#} |
| 2672 | <p> |
| 2673 | Enum literals allow specifying the name of an enum field without specifying the enum type: |
| 2674 | </p> |
| 2675 | {#code_begin|test#} |
| 2676 | const std = @import("std"); |
| 2677 | const assert = std.debug.assert; |
| 2678 | |
| 2679 | const Color = enum { |
| 2680 | Auto, |
| 2681 | Off, |
| 2682 | On, |
| 2683 | }; |
| 2684 | |
| 2685 | test "enum literals" { |
| 2686 | const color1: Color = .Auto; |
| 2687 | const color2 = Color.Auto; |
| 2688 | assert(color1 == color2); |
| 2689 | } |
| 2690 | |
| 2691 | test "switch using enum literals" { |
| 2692 | const color = Color.On; |
| 2693 | const result = switch (color) { |
| 2694 | .Auto => false, |
| 2695 | .On => true, |
| 2696 | .Off => false, |
| 2697 | }; |
| 2698 | assert(result); |
| 2699 | } |
| 2700 | {#code_end#} |
| 2701 | {#header_close#} |
| 2668 | 2702 | {#header_close#} |
| 2703 | |
| 2669 | 2704 | {#header_open|union#} |
| 2670 | 2705 | <p> |
| 2671 | 2706 | A bare {#syntax#}union{#endsyntax#} defines a set of possible types that a value |
| ... | ... | @@ -3006,7 +3041,57 @@ test "switch on tagged union" { |
| 3006 | 3041 | } |
| 3007 | 3042 | {#code_end#} |
| 3008 | 3043 | {#see_also|comptime|enum|@compileError|Compile Variables#} |
| 3044 | |
| 3045 | {#header_open|Exhaustive Switching#} |
| 3046 | <p> |
| 3047 | When a {#syntax#}switch{#endsyntax#} expression does not have an {#syntax#}else{#endsyntax#} clause, |
| 3048 | it must exhaustively list all the possible values. Failure to do so is a compile error: |
| 3049 | </p> |
| 3050 | {#code_begin|test_err|not handled in switch#} |
| 3051 | const Color = enum { |
| 3052 | Auto, |
| 3053 | Off, |
| 3054 | On, |
| 3055 | }; |
| 3056 | |
| 3057 | test "exhaustive switching" { |
| 3058 | const color = Color.Off; |
| 3059 | switch (color) { |
| 3060 | Color.Auto => {}, |
| 3061 | Color.On => {}, |
| 3062 | } |
| 3063 | } |
| 3064 | {#code_end#} |
| 3009 | 3065 | {#header_close#} |
| 3066 | |
| 3067 | {#header_open|Switching with Enum Literals#} |
| 3068 | <p> |
| 3069 | {#link|Enum Literals#} can be useful to use with {#syntax#}switch{#endsyntax#} to avoid |
| 3070 | repetitively specifying {#link|enum#} or {#link|union#} types: |
| 3071 | </p> |
| 3072 | {#code_begin|test#} |
| 3073 | const std = @import("std"); |
| 3074 | const assert = std.debug.assert; |
| 3075 | |
| 3076 | const Color = enum { |
| 3077 | Auto, |
| 3078 | Off, |
| 3079 | On, |
| 3080 | }; |
| 3081 | |
| 3082 | test "enum literals with switch" { |
| 3083 | const color = Color.Off; |
| 3084 | const result = switch (color) { |
| 3085 | .Auto => false, |
| 3086 | .On => false, |
| 3087 | .Off => true, |
| 3088 | }; |
| 3089 | assert(result); |
| 3090 | } |
| 3091 | {#code_end#} |
| 3092 | {#header_close#} |
| 3093 | {#header_close#} |
| 3094 | |
| 3010 | 3095 | {#header_open|while#} |
| 3011 | 3096 | <p> |
| 3012 | 3097 | A while loop is used to repeatedly execute an expression until |