Say I have a function that does a floating point calculations on the two arguments to the function.
As a convenience to users, hower, additional function definitions are defined so that if a user inputs an Integer, or a Rational, it gets covered to float.
For example:
function needfloat(a::T, b::T) where {T<:Real}
return a+b
end
Given that there are 2 arguments as input to the function, if I want to support all combinations of Float, Integer, and Rational arguments, all the combinations of those are quite long:
needlfoat(a::Int, b::Int) = needfloat(float(a), float(b))
needfloat(a::Int, b::AbstractFloat) = needfloat(float(a), b)
needfloat(a::AbstractFloat, b::Int) = needfloat(a, float(b))
needfloat(a::Rational, b::Rational) = needfloat(float(a), float(b))
needfloat(a::Rational, b::Int) = needfloat(float(a), float(b))
needfloat(a::Rational, b::AbstractFloat) = needfloat(float(a), b)
needfloat(a::Int, b::Rational) = needfloat(float(a), float(b))
needfloat(a::AbstractFloat, b::Rational) = needfloat(a, float(b))
My question: is there a more elegant way to do this? That is, doesn’t require defining the promotion of the arguments for every combination of Integer, Rational, and AbstractFloat?