Skip to content

Commit fc61d88

Browse files
committed
[spec/operatoroverloading] Fix comparison docs wording
Fixes #4222. Use RelExpression instead of 'inequality operators', as inequality is used to mean `!=`. Clarify that the compiler only generates a default `opEquals` for structs.
1 parent 32738eb commit fc61d88

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

spec/operatoroverloading.dd

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,19 @@ $(H2 $(LNAME2 eqcmp, Overloading the Comparison Operators))
383383
$(D x < y) nor $(D y < x) holds, but that does not imply that
384384
$(D x == y). Thus, it is insufficient to determine equality purely based on
385385
$(D opCmp) alone. For this reason, $(D opCmp) is only used for the
386-
inequality operators $(D <), $(D <=), $(D >=), and $(D >). The equality
386+
$(GLINK2 RelExpression) operators $(D <), $(D <=), $(D >=), and $(D >). The equality
387387
operators $(D ==) and $(D !=) always employ $(D opEquals) instead.)
388388

389389
$(P Therefore, it is the programmer's responsibility to ensure that
390390
`opCmp` and $(D opEquals) are consistent with each other. If
391-
`opEquals` is not specified, the compiler provides a default version
391+
`opEquals` is not specified, the compiler provides a default version for structs
392392
that does member-wise comparison. If this suffices, one may define only
393-
$(D opCmp) to customize the behaviour of the inequality operators. But
393+
$(D opCmp) to customize the behaviour of the *RelExpression* operators. But
394394
if not, then a custom version of $(D opEquals) should be defined as
395395
well, in order to preserve consistent semantics between the two kinds
396396
of comparison operators.)
397397

398-
$(P Finally, if the user-defined type is to be used as a key in the
398+
$(P Finally, if a user-defined type is to be used as a key in the
399399
built-in associative arrays, then the programmer must ensure that the
400400
semantics of $(D opEquals) and $(D toHash) are consistent. If not, the
401401
associative array may not work in the expected manner.)
@@ -538,13 +538,20 @@ struct S
538538
int opCmp(ref const S s) const { ... }
539539
}
540540
---
541-
$(P Note that $(D opCmp) is only used for the inequality operators;
542-
expressions like $(D a == b) always uses $(D opEquals). If $(D opCmp)
543-
is defined but $(D opEquals) isn't, the compiler will supply a default
544-
version of $(D opEquals) that performs member-wise comparison. If this
545-
member-wise comparison is not consistent with the user-defined
546-
`opCmp`, then it is up to the programmer to supply an appropriate
547-
version of $(D opEquals). Otherwise, inequalities like $(D a <= b)
541+
542+
$(P Note that $(D opCmp) is only used for
543+
$(GLINK2 expression, RelExpression) operators;
544+
expressions like $(D a == b) always use $(RELATIVE_LINK2 equals, `opEquals`).)
545+
546+
$(P For structs, if $(D opCmp) is defined but $(D opEquals) isn't,
547+
the compiler will supply $(DDSUBLINK spec/expression, struct_equality, a default
548+
version) of $(D opEquals) that performs member-wise comparison. However, the
549+
member-wise comparison may not be consistent with the user-defined
550+
`opCmp`.)
551+
552+
$(P It is up to the programmer to also supply a
553+
version of $(D opEquals) when appropriate. Otherwise,
554+
a *RelExpression* like $(D a <= b)
548555
will behave inconsistently with equalities like $(D a == b).)
549556

550557
$(SPEC_RUNNABLE_EXAMPLE_RUN

0 commit comments

Comments
 (0)