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

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> struct MyString

julia> a = MyString("Ilya")

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

julia> d[a] = 1

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

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

julia> import Base: ==

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

julia> a == "Ilya"

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

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

julia> import Base: hash

julia> hash(mys::MyString) = hash(
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 == 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?

you need all three.

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

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