I want two versions of a function depending on whether the argument is a Tuple{T,T}
or just T
. I thought the more specific one wins, but in the code at the end of this post, the less specific x::T
always wins over x::Tuple{T,T}
.
My goal right now is
f(x = (3,5)) # -> calls f(x::Tuple{Int,Int})
f(x = 3) # -> calls f(x::Int)
How does one do this?
A more general question is, how do you have two versions, one expecting a “scalar” and the other expecting a “collection” (array, range, tuple)?
function f(; x::Tuple{T,T}) where {T}
print("tuple ")
print(typeof(x), " ")
@show x
end
function f(; x::T) where {T}
print("single ")
print(typeof(x), " ")
@show x
end
f(x = (3,4)) # -> single Tuple{Int64, Int64} x = (3, 4)
f(x = (3,"5")) # -> single Tuple{Int64, String} x = (3, "5")
f(x = 9) # -> single Int64 x = 9