@@ -28,8 +28,8 @@ package can constitute the enum members for the enum type.
28
28
Enum member constants for a given enum type don't necessarily have to all be
29
29
declared in the same const block. Constant values may be specified using iota,
30
30
using explicit values, or by any means of declaring a valid Go const. It is
31
- allowed for multiple enum members for a given enum type to have the same
32
- constant value.
31
+ allowed for multiple enum member constants for a given enum type to have the
32
+ same constant value.
33
33
34
34
Definition of exhaustiveness
35
35
@@ -43,10 +43,10 @@ exported and unexported enum members must be listed to satisfy exhaustiveness.
43
43
For an enum type defined in an external package, it is sufficient that only
44
44
exported enum members are listed.
45
45
46
- Identifiers denoting constants (e.g. Tundra) and qualified identifiers denoting
47
- constants (e.g. somepkg.Grassland) listed in a switch statement's cases can
48
- contribute towards satisfying exhaustiveness. Literal constant values, fields in
49
- a struct value , etc. will not.
46
+ Only identifiers denoting constants (e.g. Tundra) and qualified identifiers
47
+ denoting constants (e.g. somepkg.Grassland) listed in a switch statement's cases
48
+ can contribute towards satisfying exhaustiveness. Literal values, struct fields,
49
+ re-assignable variables , etc. will not.
50
50
51
51
Type aliases
52
52
@@ -56,13 +56,13 @@ that we don't term T1 itself an enum type; it is only an alias for an enum
56
56
type.
57
57
58
58
package pkg
59
- type T1 = otherpkg .T2
59
+ type T1 = newpkg .T2
60
60
const (
61
- A = otherpkg .A
62
- B = otherpkg .B
61
+ A = newpkg .A
62
+ B = newpkg .B
63
63
)
64
64
65
- package otherpkg
65
+ package newpkg
66
66
type T2 int
67
67
const (
68
68
A T2 = 1
@@ -77,31 +77,30 @@ exported/unexported enum members apply here too.
77
77
78
78
It is worth noting that, though T1 and T2 are identical types, only constants
79
79
declared in the same scope as type T2's scope can be T2's enum members. In the
80
- example, otherpkg .A and otherpkg .B are T2's enum members.
80
+ example, newpkg .A and newpkg .B are T2's enum members.
81
81
82
82
The analyzer guarantees that introducing a type alias (such as type T1 =
83
- otherpkg .T2) will never result in new diagnostics from the analyzer, as long as
84
- the set of enum member constant values of the new RHS type (otherpkg .T2) is a
85
- subset of the set of enu member constant values of the old LHS type (T1).
83
+ newpkg .T2) will never result in new diagnostics from the analyzer, as long as
84
+ the set of enum member constant values of the new RHS type (newpkg .T2) is a
85
+ subset of the set of enum member constant values of the old LHS type (T1).
86
86
87
87
Advanced notes
88
88
89
- Recall from an earlier section that for a constant to be an enum member for an
90
- enum type, the constant must be declared in the same scope as the enum type.
91
- However it is valid, both to the Go type checker and to this analyzer, for any
92
- constant of the right type to be listed in the cases of an enum switch statement
93
- (it does not necessarily have to be a constant declared in the same scope/package
94
- as the enum type's scope/package).
95
-
89
+ Non-enum member constants in a switch statement's cases: Recall from an earlier
90
+ section that a constant must be declared in the same scope as the enum type to
91
+ be an enum member. It is valid, however, both to the Go type checker and to this
92
+ analyzer, for any constant of the right type to be listed in the cases of an
93
+ enum switch statement (it does not necessarily have to be an enum member
94
+ constant declared in the same scope/package as the enum type's scope/package).
96
95
This is particularly useful when a type alias is involved: A forwarding constant
97
- declaration (such as const pkg.A = otherpkg.A , in type T1's package, from the
98
- earlier example) can take the place of the actual enum member constant
99
- (otherpkg.A, in type T2 's package) in the switch statement's cases .
96
+ declaration (such as pkg.A, in type T1's package) can take the place of the
97
+ actual enum member constant (newpkg.A, in type T2's package) in the switch
98
+ statement 's cases, until code is migrated to use newpkg .
100
99
101
- var v pkg.T1 = pkg.ReturnsT1() // in effect, v is of type otherpkg .T2 due to alias
100
+ var v pkg.T1 = pkg.ReturnsT1() // v is effectively of type newpkg .T2 due to alias
102
101
switch v {
103
- case pkg.A: // valid substitute for otherpkg .A (same constant value)
104
- case pkg.B: // valid substitute for otherpkg .B (same constant value)
102
+ case pkg.A: // valid substitute for newpkg .A (same constant value)
103
+ case pkg.B: // valid substitute for newpkg .B (same constant value)
105
104
}
106
105
107
106
Flags
0 commit comments