user8036269
user8036269

Reputation: 67

Why Array{Float64,1} is not a subtype of Array{Real,1} in Julia?

I'm trying to write a Julia function, which can accept both 1-dimensional Int64 and Float64 array as input argument. How can I do this without defining two versions, one for Int64 and another for Float64?

I have tried using Array{Real,1} as input argument type. However, since Array{Int64,1} is not a subtype of Array{Real,1}, this cannot work.

Upvotes: 2

Views: 175

Answers (1)

Cailloumax
Cailloumax

Reputation: 260

A genuine, non secure way to do it is, with an example:


function square(x)
# The point is for element-wise operation
       out = x.*x
end


Output:

julia> square(2)
4

julia> square([2 2 2])
1×3 Array{Int64,2}:
 4  4  4

Upvotes: -1

Related Questions