Could be like this? Of course the order of evaluation of the fields should be specified somwhere, here
is just the order they appear in the struct.
julia> struct Car
model::String
year::Int32
miles::Int32
end
julia> fieldnames(Car)
(:model, :year, :miles)
julia> function isless(c1::Car,c2::Car)
for field in fieldnames(Car)
if getfield(c1,field) < getfield(c2,field)
return true
elseif getfield(c1,field) > getfield(c2,field)
return false
end
end
return false
end
isless (generic function with 1 method)
julia> c1 = Car("Fusca",1975,10_000)
Car("Fusca", 1975, 10000)
julia> c2 = Car("Chevete",1985,20_000)
Car("Chevete", 1985, 20000)
julia> isless(c1,c2)
false
julia> isless(c2,c1)
true
julia>
Edit: getfield
, if I am not mistaken, is a inherently type-unstable function. Thus, if this is going to be critical for performance, other alternatives are probably better, even if less general.
You may need to fall into this, at the end: Macro to write function with many conditionals - #8 by Skoffer