What comparison operators I need to define?



I find it inconsistent that to define equality, I should define Base.:(==) instead of Base.isequal, but to define inequality, I should define Base.isless instead of Base.:(<). What’s the rationale for this?

In general, if I define a custom struct and I define a custom equality / comparison operator, what are the methods that I should define to obtain a consistent behavior?


In operators.jl, the generic isless method for Real relies on <, so you should actually define < for a custom type, not isless, which isn’t necessary for Real subtypes (if that’s what you’re interested in).

For basic usage, ==, <, <= should suffice


Well I am confused about this. Are there guidelines written somewhere, of when and which operators I should define for consistent behavior? I suppose I need to know whether isless is defined in terms of < or the other way around, for each class of types… and so on. Of course I could just define them all, but that is not efficient :stuck_out_tongue:


But if you redefine ==, don’t you also need to (re)define hash?


Yes, but I’m not sure this entails basic usage.