authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-03 23:09:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-03 23:09:58-04:00
logc2cf04086a74a3c0ac31dbc1b8d01de76988c7ae
tree8ff02486074ecf1965470d4719a503b9b15edcb7
parentd1cda00b36d687ba2c5df463ffad30c4f7881ee9
signaturelock-open Commit is signed but in an unrecognized format.

add docs for enum literals

closes #683

1 files changed, 88 insertions(+), 3 deletions(-)

doc/langref.html.in+88-3
...@@ -2350,12 +2350,12 @@ fn doTheTest() void {...@@ -2350,12 +2350,12 @@ fn doTheTest() void {
2350 var full = Full{ .number = 0x1234 };2350 var full = Full{ .number = 0x1234 };
2351 var divided = @bitCast(Divided, full);2351 var divided = @bitCast(Divided, full);
2352 switch (builtin.endian) {2352 switch (builtin.endian) {
2353 builtin.Endian.Big => {2353 .Big => {
2354 assert(divided.half1 == 0x12);2354 assert(divided.half1 == 0x12);
2355 assert(divided.quarter3 == 0x3);2355 assert(divided.quarter3 == 0x3);
2356 assert(divided.quarter4 == 0x4);2356 assert(divided.quarter4 == 0x4);
2357 },2357 },
2358 builtin.Endian.Little => {2358 .Little => {
2359 assert(divided.half1 == 0x34);2359 assert(divided.half1 == 0x34);
2360 assert(divided.quarter3 == 0x2);2360 assert(divided.quarter3 == 0x2);
2361 assert(divided.quarter4 == 0x1);2361 assert(divided.quarter4 == 0x1);
...@@ -2630,6 +2630,8 @@ test "@tagName" {...@@ -2630,6 +2630,8 @@ test "@tagName" {
2630 assert(mem.eql(u8, @tagName(Small.Three), "Three"));2630 assert(mem.eql(u8, @tagName(Small.Three), "Three"));
2631}2631}
2632 {#code_end#}2632 {#code_end#}
2633 {#see_also|@memberName|@memberCount|@tagName|@sizeOf#}
2634
2633 {#header_open|extern enum#}2635 {#header_open|extern enum#}
2634 <p>2636 <p>
2635 By default, enums are not guaranteed to be compatible with the C ABI: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,6 +2648,7 @@ const Foo = extern enum { A, B, C };
2646export fn entry(foo: Foo) void { }2648export fn entry(foo: Foo) void { }
2647 {#code_end#}2649 {#code_end#}
2648 {#header_close#}2650 {#header_close#}
2651
2649 {#header_open|packed enum#}2652 {#header_open|packed enum#}
2650 <p>By default, the size of enums is not guaranteed.</p>2653 <p>By default, the size of enums is not guaranteed.</p>
2651 <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the2654 <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,8 +2667,40 @@ test "packed enum" {
2664 {#code_end#}2667 {#code_end#}
2665 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>2668 <p>This makes the enum eligible to be in a {#link|packed struct#}.</p>
2666 {#header_close#}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#}
2676const std = @import("std");
2677const assert = std.debug.assert;
2678
2679const Color = enum {
2680 Auto,
2681 Off,
2682 On,
2683};
2684
2685test "enum literals" {
2686 const color1: Color = .Auto;
2687 const color2 = Color.Auto;
2688 assert(color1 == color2);
2689}
2690
2691test "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 {#header_close#}2702 {#header_close#}
2703
2669 {#header_open|union#}2704 {#header_open|union#}
2670 <p>2705 <p>
2671 A bare {#syntax#}union{#endsyntax#} defines a set of possible types that a value2706 A bare {#syntax#}union{#endsyntax#} defines a set of possible types that a value
...@@ -3006,7 +3041,57 @@ test "switch on tagged union" {...@@ -3006,7 +3041,57 @@ test "switch on tagged union" {
3006}3041}
3007 {#code_end#}3042 {#code_end#}
3008 {#see_also|comptime|enum|@compileError|Compile Variables#}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#}
3051const Color = enum {
3052 Auto,
3053 Off,
3054 On,
3055};
3056
3057test "exhaustive switching" {
3058 const color = Color.Off;
3059 switch (color) {
3060 Color.Auto => {},
3061 Color.On => {},
3062 }
3063}
3064 {#code_end#}
3009 {#header_close#}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#}
3073const std = @import("std");
3074const assert = std.debug.assert;
3075
3076const Color = enum {
3077 Auto,
3078 Off,
3079 On,
3080};
3081
3082test "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 {#header_open|while#}3095 {#header_open|while#}
3011 <p>3096 <p>
3012 A while loop is used to repeatedly execute an expression until3097 A while loop is used to repeatedly execute an expression until