# Behavior of == when isless or \< is defined

**URL:** <https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114>\
**Category:** General Usage\
**Created:** [April 2, 2018, 9:02am UTC](https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114 "2018-04-02T09:02:17Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [April 4, 2018, 8:01am UTC](https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114/21 "2018-04-04T08:01:03Z")

</div>

@Tamas_Papp in that case, I stand corrected. Users have to implement `==` and `hash`. Because there are cases where `==` should not behave like `===`. Below is an example.

[https://github.com/sambitdash/IntervalTrees.jl/commit/ec83e2a49123683ac786bd27e472455501a7e15d](https://github.com/sambitdash/IntervalTrees.jl/commit/ec83e2a49123683ac786bd27e472455501a7e15d)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 4, 2018, 10:42am UTC](https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114/22 "2018-04-04T10:42:40Z")

</div>

> [@sambitdash](#):
>
> there are cases where `==` should not behave like `===`

I don’t think anyone suggested that `==` should behave like `===`; if they did, one would not need two functions.

The only connection I am aware of is that `a === b` should imply `a == b` for most cases, but note

```julia
julia> NaN == NaN
false

julia> NaN === NaN
true

```

so even that can be violated when that makes sense. The converse is of course not expected to hold in general.

Notions of equality are notoriously difficult in programming with various ramifications (eg ordering, hashing). IMO the approach that seems to work best is _practical_: define equality to have a simple mental model users can reason about, and refine this as needed by uses cases, with the understanding that sometimes it is just better to use another function for a different notion of equality.

Fortunately, doing this has zero or negligible cost for Julia, in terms of syntax (nice unicode infix ops), implementation (multiple dispatch with fallbacks), and runtime (zero-cost if done well).

Finally, probably the most important thing about various concepts of equality is _documentation_. I think this improved a lot in `v0.7`, the user can now traverse the docs of `<=`, `<`, and `isless` to learn what the semantics are. If you think they should be clarified, perhaps you could open an issue or PR.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [April 4, 2018, 11:19am UTC](https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114/23 "2018-04-04T11:19:57Z")

</div>

@Tamas_Papp in short the 0.7 behavior is different from 0.6. Most data structures when do sorting or searching depend on only one operator (mostly lessthan). The others are automatically ordered with well defined order preserving fallbacks. With the change in 0.7 there is a potential test needed to ensure `==` is behaving properly. And that has to be documented.

[Previous page](https://discourse.julialang.org/t/behavior-of-when-isless-or-is-defined/10114.md?page=1)
