Custom type comparison operator #NoobQ

question

#1

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?


#2

this would work for you.

julia> type mytype; end

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

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

#3

@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

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!


#5

Is the monkey this string?
: ( | )


#6

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


#7

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