Skip to content

Commit 9c92d64

Browse files
committed
more doc improvements
1 parent 481431f commit 9c92d64

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

exhaustive.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ package can constitute the enum members for the enum type.
2828
Enum member constants for a given enum type don't necessarily have to all be
2929
declared in the same const block. Constant values may be specified using iota,
3030
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.
3333
3434
Definition of exhaustiveness
3535
@@ -43,10 +43,10 @@ exported and unexported enum members must be listed to satisfy exhaustiveness.
4343
For an enum type defined in an external package, it is sufficient that only
4444
exported enum members are listed.
4545
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.
5050
5151
Type aliases
5252
@@ -56,13 +56,13 @@ that we don't term T1 itself an enum type; it is only an alias for an enum
5656
type.
5757
5858
package pkg
59-
type T1 = otherpkg.T2
59+
type T1 = newpkg.T2
6060
const (
61-
A = otherpkg.A
62-
B = otherpkg.B
61+
A = newpkg.A
62+
B = newpkg.B
6363
)
6464
65-
package otherpkg
65+
package newpkg
6666
type T2 int
6767
const (
6868
A T2 = 1
@@ -77,31 +77,30 @@ exported/unexported enum members apply here too.
7777
7878
It is worth noting that, though T1 and T2 are identical types, only constants
7979
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.
8181
8282
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).
8686
8787
Advanced notes
8888
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).
9695
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.
10099
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
102101
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)
105104
}
106105
107106
Flags

0 commit comments

Comments
 (0)