Skip to content

Commit b1f1c5a

Browse files
authored
Merge pull request #4223 from ntrel/opCmp-fixes
[spec/operatoroverloading] Fix comparison docs wording
2 parents 8b74b7e + d7bee22 commit b1f1c5a

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

spec/expression.dd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ $(GNAME EqualExpression):
681681

682682
$(P For struct objects, equality means the result of the
683683
$(LINK2 https://dlang.org/spec/operatoroverloading.html#equals, `opEquals()` member function).
684-
If an `opEquals()` is not provided, equality is defined as
685-
the logical product of all equality
684+
If an `opEquals()` is not provided, one will be generated.
685+
Equality is defined as the logical product of all equality
686686
results of the corresponding object fields.
687687
)
688688

spec/operatoroverloading.dd

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,20 @@ $(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 for a struct, the compiler provides a
392+
$(DDSUBLINK spec/expression, struct_equality, default version) of it
392393
that does member-wise comparison. If this suffices, one may define only
393-
$(D opCmp) to customize the behaviour of the inequality operators. But
394+
$(D opCmp) to customize the behaviour of the *RelExpression* operators. But
394395
if not, then a custom version of $(D opEquals) should be defined as
395396
well, in order to preserve consistent semantics between the two kinds
396397
of comparison operators.)
397398

398-
$(P Finally, if the user-defined type is to be used as a key in the
399+
$(P Finally, if a user-defined type is to be used as a key in the
399400
built-in associative arrays, then the programmer must ensure that the
400401
semantics of $(D opEquals) and $(D toHash) are consistent. If not, the
401402
associative array may not work in the expected manner.)
@@ -562,13 +563,20 @@ struct S
562563
int opCmp(ref const S s) const { ... }
563564
}
564565
---
565-
$(P Note that $(D opCmp) is only used for the inequality operators;
566-
expressions like $(D a == b) always uses $(D opEquals). If $(D opCmp)
567-
is defined but $(D opEquals) isn't, the compiler will supply a default
568-
version of $(D opEquals) that performs member-wise comparison. If this
569-
member-wise comparison is not consistent with the user-defined
570-
`opCmp`, then it is up to the programmer to supply an appropriate
571-
version of $(D opEquals). Otherwise, inequalities like $(D a <= b)
566+
567+
$(P Note that $(D opCmp) is only used for
568+
$(GLINK2 expression, RelExpression) operators;
569+
expressions like $(D a == b) always use $(RELATIVE_LINK2 equals, `opEquals`).)
570+
571+
$(P For structs, if $(D opCmp) is defined but $(D opEquals) isn't,
572+
the compiler will supply $(DDSUBLINK spec/expression, struct_equality, a default
573+
version) of $(D opEquals) that performs member-wise comparison. However, the
574+
member-wise comparison may not be consistent with the user-defined
575+
`opCmp`.)
576+
577+
$(P It is up to the programmer to also supply a
578+
version of $(D opEquals) when appropriate. Otherwise,
579+
a *RelExpression* like $(D a <= b)
572580
will behave inconsistently with equalities like $(D a == b).)
573581

574582
$(SPEC_RUNNABLE_EXAMPLE_RUN

0 commit comments

Comments
 (0)