Custom type comparison operator #NoobQ

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?

1 Like

this would work for you.

julia> type mytype; end

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

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

@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

already provides everything unless you need something special.

4 Likes

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!

Is the monkey this string?
: ( | )

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

\: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.

1 Like