Skip to content

Commit a9b34bb

Browse files
committed
Document the guarantees that should be provided by isequal
1 parent e36fe95 commit a9b34bb

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

base/operators.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ should therefore also implement [`hash`](@ref).
8888
Similar to [`==`](@ref), except for the treatment of floating point numbers
8989
and of missing values. `isequal` treats all floating-point `NaN` values as equal
9090
to each other, treats `-0.0` as unequal to `0.0`, and [`missing`](@ref) as equal
91-
to `missing`. Always returns a `Bool` value.
91+
to `missing`. Always returns a `Bool` value. It can be assumed that two values which
92+
are `===` are also `isequal`, and that `isequal(x, y)` returns the same result as
93+
`isequal(y, x)`.
9294
9395
# Implementation
9496
The default implementation of `isequal` calls `==`, so a type that does not involve
@@ -98,8 +100,12 @@ floating-point values generally only needs to define `==`.
98100
that `hash(x) == hash(y)`.
99101
100102
This typically means that types for which a custom `==` or `isequal` method exists must
101-
implement a corresponding `hash` method (and vice versa). Collections typically implement
102-
`isequal` by calling `isequal` recursively on all contents.
103+
implement a corresponding [`hash`](@ref) method (and vice versa). Collections typically
104+
implement `isequal` by calling `isequal` recursively on all contents.
105+
106+
Furthermore, `isequal` is linked with [`isless`](@ref), and they work together to
107+
define a fixed total ordering, where only one of `isequal(x, y)`, `isless(x, y)`, or
108+
`isless(y, x)` may be `true` (and the other two `false`).
103109
104110
Scalar types generally do not need to implement `isequal` separate from `==`, unless they
105111
represent floating-point numbers amenable to a more efficient implementation than that
@@ -132,8 +138,8 @@ isequal(x::AbstractFloat, y::Real ) = (isnan(x) & isnan(y)) | signequal(
132138
"""
133139
isless(x, y)
134140
135-
Test whether `x` is less than `y`, according to a fixed total order.
136-
`isless` is not defined on all pairs of values `(x, y)`. However, if it
141+
Test whether `x` is less than `y`, according to a fixed total order (defined together with
142+
[`isequal`](@ref)). `isless` is not defined on all pairs of values `(x, y)`. However, if it
137143
is defined, it is expected to satisfy the following:
138144
- If `isless(x, y)` is defined, then so is `isless(y, x)` and `isequal(x, y)`,
139145
and exactly one of those three yields `true`.

0 commit comments

Comments
 (0)