# Weird hash behavior

**URL:** <https://discourse.julialang.org/t/weird-hash-behavior/3854>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 22, 2017, 3:02pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854 "2017-05-22T15:02:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 22, 2017, 3:02pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854/1 "2017-05-22T15:02:25Z")

</div>

I have a custom type, and I defined `==` for it. I’m trying to `delete!` one member in a `OrderedDict` but it doesn’t work. I traced down the problem to the following behavior:

```julia
using DataStructures
import Base: ==
n = 2
type A
    x::Int
end
==(a1::A, a2::A) = a1.x == a2.x
a = OrderedSet(A(i - 3) for i = 1:n)
a1 = A(-2)
@assert a1 == a[1] # checks out
@assert hash(a1) == hash(a[1]) # doesn't!

```

So why does the hash of `a1` and `a[1]` not the same? Or how can I `delete!` things that are `==`?

Thanks!

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 22, 2017, 3:10pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854/2 "2017-05-22T15:10:06Z")

</div>

The default `hash` of a `type` object is the hash of its pointer. `a1 === a[1]` is false (they are different objects) so they hash to different values. [You should usually define `Base.hash` whenever you define a custom `==`](https://docs.julialang.org/en/stable/stdlib/base/#Base.isequal).

Or you could use an `immutable`; then the default equality and hash are already what you want.

---

<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:** [May 22, 2017, 3:40pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854/3 "2017-05-22T15:40:08Z")

</div>

For most cases [AutoHashEquals.jl](https://github.com/andrewcooke/AutoHashEquals.jl) is a very compact solution.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 22, 2017, 5:34pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854/4 "2017-05-22T17:34:19Z")

</div>

Ah…! I see. Thanks!

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 22, 2017, 5:40pm UTC](https://discourse.julialang.org/t/weird-hash-behavior/3854/5 "2017-05-22T17:40:18Z")

</div>

OK, `immutable` just takes care of all of that. But I might not be able to implement a immutable type conveniently, in which case I’ll use AutoHashEquals.jl.
