# Suitability of overriding == (vs ===)?

**URL:** <https://discourse.julialang.org/t/suitability-of-overriding-vs/2478>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 5, 2017, 9:03pm UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478 "2017-03-05T21:03:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [March 5, 2017, 9:03pm UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478/1 "2017-03-05T21:03:04Z")

</div>

Some advice, please. Consider a structure like this:

```julia
immutable Foo
  x::Int
  y::Int
  s::String
end

```

(In the past, this type didn’t have `s` so `==` just compared `x` and `y`.)

I’d still like to have `==` compare `x` and `y`, but ignore `s`. Because `Foo` is immutable, `===` will compare all fields (which is useful as well). I was hoping to override `==` as follows:

```julia
== (a::Foo, b::Foo) = (a.x == b.x && a.y == b.y)

```

This way, `Foo(1,2,"foo") == Foo(1,2,"bar")` is `true`, but `Foo(1,2,"foo") === Foo(1,2,"bar")` is `false`.

Are there any unintended consequences to overriding `==` like this? (I am not doing anything with hashcodes or anything.)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 5, 2017, 9:19pm UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478/2 "2017-03-05T21:19:59Z")

</div>

Remember to overload the `hash` if you want to use any functions that uses `hash`. Should be fine otherwise. (Other than that `Foo(1,2,"foo") == Foo(1,2,"bar")` is a bit weird without context.)

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [March 5, 2017, 9:34pm UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478/3 "2017-03-05T21:34:27Z")

</div>

Thanks, @yuyichao. Under what circumstances would `hash` need to be overriden?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 5, 2017, 10:41pm UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478/4 "2017-03-05T22:41:06Z")

</div>

> [@anon94023334](#):
>
> Under what circumstances would hash need to be overriden?

> if you want to use any functions that uses hash

[http://docs.julialang.org/en/latest/search.html?q=hash](http://docs.julialang.org/en/latest/search.html?q=hash)

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [May 18, 2017, 9:18am UTC](https://discourse.julialang.org/t/suitability-of-overriding-vs/2478/5 "2017-05-18T09:18:27Z")

</div>

Maybe @anon94023334 , this package [AutoHashEquals](https://github.com/andrewcooke/AutoHashEquals.jl) will be helpful.
