As a beginner programmer who’s relatively new to Julia, I’m a bit confused about how method definitions work. If I define a simple function as follows:
function xy{T<:Real}(x::Vector{T}, y::Vector{T})
out = x .* y
end
Why do I get a MethodError if, for example, x is of type Int64 and y is of type Float64? There’s a similar example in the Methods documentation, but it’s still not clear to me why both types can’t be used in the function if they are both subtypes of Real. This may stem from my misunderstanding of how type parameters are used in the method signature.
I often work with data that can be of various types and it would be rather cumbersome to convert them all to similar types prior to executing a function. For example, the function above wouldn’t work if x was of type Float32 and y was of type Float64. A trivial solution would be a more generalized form of the function:
function xy(x::Vector, y::Vector)
out = x .* y
end
However, I feel that this isn’t ideal. If I wanted to add a two-dimensional array to the parameter tuple, I’m not sure how I would explicitly enforce the dimensionality of the array without the presence of a method type parameter T. For example, a function with definition as follows:
function xyz{T<:Real}(x::Vector{T}, y::Vector{T}, z::Array{T,2})
would require z to be a 2-dimensional array, but also require it have the same type as x and y.
I was hoping someone could provide me with some clarity on the technical aspects involved with this. And more importantly, what is the recommended “Julian” way of proceeding if x, y, and z are of different types?