# How to make dict lookups work with an overloaded == and hash function

**URL:** https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098
**Category:** New to Julia
**Created:** [October 1, 2022, 10:24pm UTC](https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098 "2022-10-01T22:24:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)
#### Post date: [October 1, 2022, 10:24pm UTC](https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098/1 "2022-10-01T22:24:30Z")

</div>

I have a custom type that includes a string as it’s unique ID. I would like to simplify operations by permitting lookups of this object by just the String. I proceed with the basic construction and of course it doesn’t work by default.

```julia
julia> struct MyString
       name::String
       end

julia> a = MyString("Ilya")
MyString("Ilya")

julia> d = Dict{MyString,Integer}()
Dict{MyString, Integer}()

julia> d[a] = 1
1

julia> d["Ilya"]
ERROR: KeyError: key "Ilya" not found

```

Easy, I’ll overload equals and everything should be dandy:

```julia
julia> import Base: ==

julia> ==(x::MyString, y::String) = x.name == y
== (generic function with 225 methods)

julia> a == "Ilya"
true

julia> d["Ilya"]
ERROR: KeyError: key "Ilya" not found

```

Hmm, maybe I need to overload hash() as well?

```julia
julia> import Base: hash

julia> hash(mys::MyString) = hash(mys.name)
hash (generic function with 88 methods)

julia> d["Ilya"]
ERROR: KeyError: key "Ilya" not found

```

So I was pretty stumped at this point, but then I tried overloading `==(x::String, y::MyString) = x == y.name` and everything worked beautifully.

My question is what is the correct way to implement this behavior? Do I need to overload all three functions or can I get away with just one?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 1, 2022, 11:19pm UTC](https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098/2 "2022-10-01T23:19:55Z")

</div>

you need all three.

---

<div class="post-metadata">

### Author: ![ivoytov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivoytov/32/42480_2.png) [@ivoytov](https://discourse.julialang.org/u/ivoytov)
#### Post date: [October 2, 2022, 12:44am UTC](https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098/3 "2022-10-02T00:44:12Z")

</div>

so if I want to add `MyString` to sorted lists, do I also need to overload both `<` and `>`?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 2, 2022, 12:51am UTC](https://discourse.julialang.org/t/how-to-make-dict-lookups-work-with-an-overloaded-and-hash-function/88098/4 "2022-10-02T00:51:37Z")

</div>

the docs for sorting specify that sorting uses `isless` (which falls back to `<`)
