# Custom type comparison operator #NoobQ

**URL:** <https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 16, 2017, 6:05pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313 "2017-06-16T18:05:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nectarineimp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nectarineimp/32/883_2.png) [@Nectarineimp](https://discourse.julialang.org/u/Nectarineimp)\
**Post date:** [June 16, 2017, 6:05pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/1 "2017-06-16T18:05:08Z")

</div>

How do I create custom type comparison operators?

In the documentation it talks about making a custom ==() function, but when I try this (simplified example)

```
Base.==(x::mytype, y::mytype) = true;

```

It doesn’t recognize the == as the name. I noticed that isequal is not the same as == as well.

So how do I overload operators to work with my custom types?

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [June 16, 2017, 7:15pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/2 "2017-06-16T19:15:51Z")

</div>

this would work for you.

```julia
julia> type mytype; end

julia> Base.:(==)(x::mytype, y::mytype) = :🙈

julia> mytype() == mytype()
:🙈

```

---

<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:** [June 17, 2017, 6:39am UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/3 "2017-06-17T06:39:31Z")

</div>

@wookyoung: Please don’t answer newbie questions with cute stuff like `🙈`, it may be confusing.

@Nectarineimp: Also, don’t forget `Base.hash`. You may find that the functionality of

> **[GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash()...](https://github.com/JuliaServices/AutoHashEquals.jl)**
>
> A Julia macro to add == and hash() to composite types. - GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash() to composite types.

already provides everything unless you need something special.

---

<div class="post-metadata">

**Author:** ![Nectarineimp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nectarineimp/32/883_2.png) [@Nectarineimp](https://discourse.julialang.org/u/Nectarineimp)\
**Post date:** [June 19, 2017, 6:15pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/4 "2017-06-19T18:15:38Z")

</div>

Thanks both of you. I didn’t mind the monkey, and I think it’s just an auto translation done by discourse!

I will look into AutoHashEquals. What I need is a way to treat a custom type like any other basic type. This will allow a custom type to work with Dict and sort and so forth.

Thanks!

---

<div class="post-metadata">

**Author:** ![Nectarineimp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nectarineimp/32/883_2.png) [@Nectarineimp](https://discourse.julialang.org/u/Nectarineimp)\
**Post date:** [June 19, 2017, 6:17pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/5 "2017-06-19T18:17:24Z")

</div>

Is the monkey this string?  
: ( | )

---

<div class="post-metadata">

**Author:** ![Nectarineimp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nectarineimp/32/883_2.png) [@Nectarineimp](https://discourse.julialang.org/u/Nectarineimp)\
**Post date:** [June 19, 2017, 6:55pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/6 "2017-06-19T18:55:10Z")

</div>

Just tested my code with the AutoHashEquals and it works great. You just saved me a lot of work. Thank you so much.

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [June 20, 2017, 12:24pm UTC](https://discourse.julialang.org/t/custom-type-comparison-operator-noobq/4313/7 "2017-06-20T12:24:12Z")

</div>

`\:see_no_evil:` with `<TAB>`

to quote about that emoji,

> The three wise monkeys usually used to describe someone who doesn’t want to be involved in a situation

for something about the custom behavior in that situation.
