@@ -38,23 +38,6 @@ of the enum type's members are listed in the switch statement's cases. If
38
38
multiple enum member constants have the same constant value, it is sufficient
39
39
for any one of these same-valued members to be listed.
40
40
41
- type Command int
42
-
43
- const (
44
- Build Command = iota
45
- Test
46
- Default = Build
47
- )
48
-
49
- func f(cmd Command) {
50
- // This switch statement is exhaustive. (The enum member Default does
51
- // not have to be listed, because it has the same value as Build.)
52
- switch cmd {
53
- case Build:
54
- case Test:
55
- }
56
- }
57
-
58
41
For an enum type defined in the same package as the switch statement, both
59
42
exported and unexported enum members must be listed to satisfy exhaustiveness.
60
43
For an enum type defined in an external package, it is sufficient that only
@@ -104,18 +87,15 @@ constant of the right type to be listed in the cases of an enum switch statement
104
87
(it does not necessarily have to be a constant declared in the same scope/package
105
88
as the enum type's scope/package).
106
89
107
- Such a constant can contribute towards satisfying switch statement
108
- exhaustiveness if it has the same constant value as an actual enum member
109
- constant. The constant can be a substitute for the enum member constant in the
110
- switch statement's cases. This behavior is particularly useful when a type alias
111
- is involved: A constant (such as pkg.A, in type T1's package) can take the place
112
- of the actual enum member constant (such as otherpkg.A, in type T2's package) in
113
- the switch statement's cases.
90
+ This is particularly useful when a type alias is involved: A forwarding constant
91
+ declaration (such as const pkg.A = otherpkg.A, in type T1's package, from the
92
+ earlier example) can take the place of the actual enum member constant
93
+ (otherpkg.A, in type T2's package) in the switch statement's cases.
114
94
115
95
var v pkg.T1 = pkg.ReturnsT1() // in effect, v is of type otherpkg.T2 due to alias
116
96
switch v {
117
- case pkg.A: // can be a substitute for otherpkg.A (both have same value)
118
- case pkg.B: // can be a substitute for otherpkg.B (both have same value)
97
+ case pkg.A: // valid substitute for otherpkg.A (same constant value)
98
+ case pkg.B: // valid substitute for otherpkg.B (same constant value)
119
99
}
120
100
121
101
Flags
0 commit comments