1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Version of November 21 , 2022",
3
+ "Subtitle": "Version of December 14 , 2022",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -2684,18 +2684,17 @@ <h4 id="Type_constraints">Type constraints</h4>
2684
2684
The < a href ="#Predeclared_identifiers "> predeclared</ a >
2685
2685
< a href ="#Interface_types "> interface type</ a > < code > comparable</ code >
2686
2686
denotes the set of all non-interface types that are
2687
- < a href ="#Comparison_operators "> comparable</ a > . Specifically,
2687
+ < a href ="#Comparison_operators "> strictly comparable</ a > . Specifically,
2688
2688
a type < code > T</ code > implements < code > comparable</ code > if:
2689
2689
</ p >
2690
2690
2691
2691
< ul >
2692
2692
< li >
2693
- < code > T</ code > is not an interface type and < code > T</ code > supports the operations
2694
- < code > ==</ code > and < code > !=</ code > ; or
2693
+ < code > T</ code > is not an interface type and < code > T</ code > is strictly comparable; or
2695
2694
</ li >
2696
2695
< li >
2697
2696
< code > T</ code > is an interface type and each type in < code > T</ code > 's
2698
- < a href ="#Interface_types "> type set</ a > implements < code > comparable</ code > .
2697
+ < a href ="#Interface_types "> type set</ a > is strictly comparable.
2699
2698
</ li >
2700
2699
</ ul >
2701
2700
@@ -2707,12 +2706,13 @@ <h4 id="Type_constraints">Type constraints</h4>
2707
2706
</ p >
2708
2707
2709
2708
< pre >
2710
- int // implements comparable
2709
+ int // implements comparable (int is strictly comparable)
2711
2710
[]byte // does not implement comparable (slices cannot be compared)
2712
2711
interface{} // does not implement comparable (see above)
2713
- interface{ ~int | ~string } // type parameter only: implements comparable
2714
- interface{ comparable } // type parameter only: implements comparable
2715
- interface{ ~int | ~[]byte } // type parameter only: does not implement comparable (not all types in the type set are comparable)
2712
+ interface{ ~int | ~string } // type parameter only: implements comparable (int, string types are stricly comparable)
2713
+ interface{ comparable } // type parameter only: implements comparable (comparable implements itself)
2714
+ interface{ ~int | ~[]byte } // type parameter only: does not implement comparable (slices are not comparable)
2715
+ interface{ ~struct{ any } } // type parameter only: does not implement comparable (field any is not strictly comparable)
2716
2716
</ pre >
2717
2717
2718
2718
< p >
@@ -5019,93 +5019,99 @@ <h3 id="Comparison_operators">Comparison operators</h3>
5019
5019
</ p >
5020
5020
< p >
5021
5021
The equality operators < code > ==</ code > and < code > !=</ code > apply
5022
- to operands that are < i > comparable</ i > .
5022
+ to operands of < i > comparable</ i > types .
5023
5023
The ordering operators < code > <</ code > , < code > <=</ code > , < code > ></ code > , and < code > >=</ code >
5024
- apply to operands that are < i > ordered</ i > .
5024
+ apply to operands of < i > ordered</ i > types .
5025
5025
These terms and the result of the comparisons are defined as follows:
5026
5026
</ p >
5027
5027
5028
5028
< ul >
5029
5029
< li >
5030
- Boolean values are comparable.
5030
+ Boolean types are comparable.
5031
5031
Two boolean values are equal if they are either both
5032
5032
< code > true</ code > or both < code > false</ code > .
5033
5033
</ li >
5034
5034
5035
5035
< li >
5036
- Integer values are comparable and ordered, in the usual way.
5036
+ Integer types are comparable and ordered.
5037
+ Two integer values are compared in the usual way.
5037
5038
</ li >
5038
5039
5039
5040
< li >
5040
- Floating-point values are comparable and ordered,
5041
- as defined by the IEEE-754 standard.
5041
+ Floating-point types are comparable and ordered.
5042
+ Two floating-point values are compared as defined by the IEEE-754 standard.
5042
5043
</ li >
5043
5044
5044
5045
< li >
5045
- Complex values are comparable.
5046
+ Complex types are comparable.
5046
5047
Two complex values < code > u</ code > and < code > v</ code > are
5047
5048
equal if both < code > real(u) == real(v)</ code > and
5048
5049
< code > imag(u) == imag(v)</ code > .
5049
5050
</ li >
5050
5051
5051
5052
< li >
5052
- String values are comparable and ordered, lexically byte-wise.
5053
+ String types are comparable and ordered.
5054
+ Two string values are compared lexically byte-wise.
5053
5055
</ li >
5054
5056
5055
5057
< li >
5056
- Pointer values are comparable.
5058
+ Pointer types are comparable.
5057
5059
Two pointer values are equal if they point to the same variable or if both have value < code > nil</ code > .
5058
5060
Pointers to distinct < a href ="#Size_and_alignment_guarantees "> zero-size</ a > variables may or may not be equal.
5059
5061
</ li >
5060
5062
5061
5063
< li >
5062
- Channel values are comparable.
5064
+ Channel types are comparable.
5063
5065
Two channel values are equal if they were created by the same call to
5064
5066
< a href ="#Making_slices_maps_and_channels "> < code > make</ code > </ a >
5065
5067
or if both have value < code > nil</ code > .
5066
5068
</ li >
5067
5069
5068
5070
< li >
5069
- Interface values are comparable.
5071
+ Interface types that are not type parameters are comparable.
5070
5072
Two interface values are equal if they have < a href ="#Type_identity "> identical</ a > dynamic types
5071
5073
and equal dynamic values or if both have value < code > nil</ code > .
5072
5074
</ li >
5073
5075
5074
5076
< li >
5075
5077
A value < code > x</ code > of non-interface type < code > X</ code > and
5076
- a value < code > t</ code > of interface type < code > T</ code > are comparable when values
5077
- of type < code > X</ code > are comparable and
5078
+ a value < code > t</ code > of interface type < code > T</ code > can be compared
5079
+ if type < code > X</ code > is comparable and
5078
5080
< code > X</ code > < a href ="#Implementing_an_interface "> implements</ a > < code > T</ code > .
5079
5081
They are equal if < code > t</ code > 's dynamic type is identical to < code > X</ code >
5080
5082
and < code > t</ code > 's dynamic value is equal to < code > x</ code > .
5081
5083
</ li >
5082
5084
5083
5085
< li >
5084
- Struct values are comparable if all their fields are comparable.
5086
+ Struct types are comparable if all their field types are comparable.
5085
5087
Two struct values are equal if their corresponding
5086
5088
non-< a href ="#Blank_identifier "> blank</ a > field values are equal.
5087
5089
The fields are compared in source order, and comparison stops as
5088
5090
soon as two field values differ (or all fields have been compared).
5089
5091
</ li >
5090
5092
5091
5093
< li >
5092
- Array values are comparable if values of the array element type are comparable.
5094
+ Array types are comparable if their array element types are comparable.
5093
5095
Two array values are equal if their corresponding element values are equal.
5094
5096
The elements are compared in ascending index order, and comparison stops
5095
5097
as soon as two element values differ (or all elements have been compared).
5096
5098
</ li >
5099
+
5100
+ < li >
5101
+ Type parameters are comparable if they are strictly comparable (see below).
5102
+ </ li >
5097
5103
</ ul >
5098
5104
5099
5105
< p >
5100
5106
A comparison of two interface values with identical dynamic types
5101
- causes a < a href ="#Run_time_panics "> run-time panic</ a > if values
5102
- of that type are not comparable. This behavior applies not only to direct interface
5107
+ causes a < a href ="#Run_time_panics "> run-time panic</ a > if that type
5108
+ is not comparable. This behavior applies not only to direct interface
5103
5109
value comparisons but also when comparing arrays of interface values
5104
5110
or structs with interface-valued fields.
5105
5111
</ p >
5106
5112
5107
5113
< p >
5108
- Slice, map, and function values are not comparable.
5114
+ Slice, map, and function types are not comparable.
5109
5115
However, as a special case, a slice, map, or function value may
5110
5116
be compared to the predeclared identifier < code > nil</ code > .
5111
5117
Comparison of pointer, channel, and interface values to < code > nil</ code >
@@ -5126,6 +5132,30 @@ <h3 id="Comparison_operators">Comparison operators</h3>
5126
5132
)
5127
5133
</ pre >
5128
5134
5135
+ < p >
5136
+ A type is < i > strictly comparable</ i > if it is comparable and not an interface
5137
+ type nor composed of interface types.
5138
+ Specifically:
5139
+ </ p >
5140
+
5141
+ < ul >
5142
+ < li >
5143
+ Boolean, numeric, string, pointer, and channel types are strictly comparable.
5144
+ </ li >
5145
+
5146
+ < li >
5147
+ Struct types are strictly comparable if all their field types are strictly comparable.
5148
+ </ li >
5149
+
5150
+ < li >
5151
+ Array types are strictly comparable if their array element types are strictly comparable.
5152
+ </ li >
5153
+
5154
+ < li >
5155
+ Type parameters are strictly comparable if all types in their type set are strictly comparable.
5156
+ </ li >
5157
+ </ ul >
5158
+
5129
5159
< h3 id ="Logical_operators "> Logical operators</ h3 >
5130
5160
5131
5161
< p >
0 commit comments