Skip to content

Commit

Permalink
update docs for == and isequal
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed May 2, 2014
1 parent f9cb1e3 commit b5e0b73
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,34 +139,19 @@ All Objects

.. function:: isequal(x, y)

True if and only if ``x`` and ``y`` would cause a typical function to behave the
same. A "typical" function is one that uses only intended interfaces, and does not
unreasonably exploit implementation details of its arguments. For example,
floating-point ``NaN`` values are ``isequal`` regardless of their sign bits, since
the sign of a ``NaN`` has no meaning in the vast majority of cases (but can be
discovered if you really want to).

One implication of this definition is that implementing ``isequal`` for a new type
encapsulates, to a large extent, what the true abstraction presented by that type
is. For example, a ``String`` is a sequence of characters, so two strings are
``isequal`` if they generate the same characters. Other concerns, such as encoding,
are not considered.

When calling ``isequal``, be aware that it cannot be all things to all people.
For example, if your use of strings *does* care about encoding, you will have to
perform a check like ``typeof(x)==typeof(y) && isequal(x,y)``.

``isequal`` is the default comparison function used by hash tables (``Dict``).
``isequal(x,y)`` must imply ``hash(x)==hash(y)``.
Similar to ``==``, except treats all floating-point ``NaN`` values as equal, and
treats ``-0.0`` as unequal to ``0.0``. Falls back to ``==``.

New types with a notion of equality should implement this function, except for
numbers, which should implement ``==`` instead. However, numeric types with special
values like ``NaN`` might need to implement ``isequal`` as well. Numbers of different
types are considered unequal.
``isequal`` is the comparison function used by hash tables (``Dict``).
``isequal(x,y)`` must imply ``hash(x)==hash(y)``.

Mutable containers should generally implement ``isequal`` by calling ``isequal``
recursively on all contents.

Other new types generally do not need to implement this function, unless they
represent floating-point numbers amenable to a more efficient implementation
than that provided by a generic fallback (based on ``isnan``, ``signbit``, and ``==``).

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski May 2, 2014

Member

This is unfortunately not true for collections since we just fall back on == which is incorrect in that case. This pull request doesn't attempt to fix that pre-existing situation, but I think that we really ought to fix this. The real fix may be to provide the generic ability to recursively compare two structures and define both == and isequal on collections in terms of that. Then we can let people override this when they have a new collection type.

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson May 2, 2014

Author Member

OK, how about I amend the prior paragraph to say "collections" instead of "mutable containers"?

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski May 2, 2014

Member

Oh, I kind of missed this this was other new types as opposed to "mutable containers" and was thinking as opposed to floating-point types. Maybe just make that bit more explicit – something like "scalar types generally do not..."


.. function:: isless(x, y)

Test whether ``x`` is less than ``y``, according to a canonical total order.
Expand Down Expand Up @@ -2288,8 +2273,18 @@ Mathematical Operators
.. _==:
.. function:: ==(x, y)

Numeric equality operator. Compares numbers and number-like values (e.g. arrays) by numeric value. True for numbers of different types that represent the same value (e.g. ``2`` and ``2.0``). Follows IEEE semantics for floating-point numbers.
New numeric types should implement this function for two arguments of the new type.
Generic equality operator, giving a single ``Bool`` result. Falls back to ``===``.
Should be implemented for all types with a notion of equality, based
on the abstract value that an instance represents. For example, all numeric types are compared
by numeric value, ignoring type. Strings are compared as sequences of characters, ignoring
encoding.

Follows IEEE semantics for floating-point numbers.

Mutable containers should generally implement ``==`` by calling ``==`` recursively on all contents.

New numeric types should implement this function for two arguments of the new type, and handle
comparison to other types via promotion rules where possible.

.. _!=:
.. function:: !=(x, y)
Expand Down

0 comments on commit b5e0b73

Please sign in to comment.