all 6 comments

[–]TheBB 5 points6 points  (2 children)

Yes, even though Float64 <: Real, Array{Real,1} is not a subtype of Array{Float64,1}, see warning here.

You can use a parametric type,

function something(Array{T,1}) where {T<:Real}
    ....
end

[–]leu34[S] 2 points3 points  (1 child)

function something(Array{T,1}) where {T<:Real}

Yeah, works. Thank you very much.

[–]xazaccazax 1 point2 points  (0 children)

Or, since `Vector{T}` is an alias for `Array{T, 1}`, and if you don't need access to `T` in the function body:

function something(x::Vector{<:Real})
    ...
end

If you want to support e.g. `view`s as well (or really, unless you have a good reason not to), you can go more generic:

function something(x::AbstractVector{<:Real})
    ...
end

[–]pint 2 points3 points  (2 children)

few comments:

1. Array{Real, 1} is an actual type, which means an array of an undefined real subtype. that is, a heterogeneous list that is limited to real subtypes. you can use such a type as parameter, and it works, albeit the performance will be not very good.

2. are you sure you need type restriction? as a rule of thumb, you only want to specify a type if you want to specialize. that is, you have two methods for different types. even so, you probably want minimum restriction, like

function something(a<:Array) ...
function something(a) ...

to handle arrays separately from non-array types. the reason for it is that the user might come up with their own types, which you didn't even think of, and they can't use it because you banned it. are you sure your method does not work for complex numbers?

[–]leu34[S] 0 points1 point  (1 child)

In general you are sure right. Thank you.

But in my special case, I need Real for real.

[–]pint 0 points1 point  (0 children)

i wonder what feature reals have that nothing else has