# Multiple dispatch of \`in\`

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-of-in/85227>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 3, 2022, 12:32pm UTC](https://discourse.julialang.org/t/multiple-dispatch-of-in/85227 "2022-08-03T12:32:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 3, 2022, 12:32pm UTC](https://discourse.julialang.org/t/multiple-dispatch-of-in/85227/1 "2022-08-03T12:32:13Z")

</div>

I’d like to customise how `in` works for a struct I’ve created as only some fields matter. I’d like it to work like this where here I just want to check the dogs breeds:

```nohighlight
struct Dog
    breed
    name
end

a = Dog("husky", "a")
b = Dog("husky", "b")
c = Dog("labrador", "c")

dogs = [b]
a in dogs # true
c in dogs # false

```

I think `in` uses `Base.:==` but I’m not sure how to extend this? Extending `Base.isequal` doesn’t have the correct affect.

How can I do this?

N.B. in the Python world this would the implementation of ` __eq__ `

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2022, 12:39pm UTC](https://discourse.julialang.org/t/multiple-dispatch-of-in/85227/2 "2022-08-03T12:39:43Z")

</div>

> [@this\_josh](#):
>
> I think `in` uses `Base.:==` but I’m not sure how to extend this?

```julia
Base.:(==)(a::Dog, b::Dog) = a.breed == b.breed
Base.hash(a::Dog, h::UInt) = hash(a.breed, h)

```

where if you change the definition of equality you also need to change `Base.hash` so that hash tables (e.g. `Set` data structures) work. (By default, `isequal` calls `==`.)

Alternatively, you define your own `in`-like function (e.g. the `∈ᵇ` operator) to be whatever you want, while leaving equality as-is.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 3, 2022, 12:48pm UTC](https://discourse.julialang.org/t/multiple-dispatch-of-in/85227/3 "2022-08-03T12:48:01Z")

</div>

> [@stevengj](#):
>
> `(==)`

Thank you I was missing the parentheses around `==`. I’ve chosen to extend `==` as this means I get the `in` functionality for free. Maybe I’ll come to regret this, but such is learning!
