New dispatch for isless() does not work

I need to be able to compare instances of a struct named Hijri, so I have defined a new method for the isless() function as follows:

function isless(a::Hijri, b::Hijri)
	tuple_a = datetuple(a)
	tuple_b = datetuple(b)
	
	return tuple_a < tuple_b
end

datetuple() returns values of the Hijri struct as a tuple.

This works fine:

@show isless(a, b)

However, this one

@show a < b

throws an error:

ERROR: LoadError: MethodError: no method matching isless(::Main.HijriConverter.Hijri, ::Main.HijriConverter.Hijri)
Closest candidates are:
  isless(::Any, ::Missing) at ~/Downloads/julia-1.7.0/share/julia/base/missing.jl:88
  isless(::Missing, ::Any) at ~/Downloads/julia-1.7.0/share/julia/base/missing.jl:87
Stacktrace:
 [1] <(x::Main.HijriConverter.Hijri, y::Main.HijriConverter.Hijri)
   @ Base ./operators.jl:352
 [2] top-level scope
   @ show.jl:1047
in expression starting at /home/jafar_isbarov/Documents/projects/hijri/HijriConverter.jl/src/HijriConverter.jl:1

What could be the reason?

You may have forgotten to import isless from Base or you can use Base.isless in your function definition.

use this before isless definition


import Base: isless

or define the function as below to extend existing definition in Base


function Base.isless(...)

end
3 Likes

Thanks!