Array of type Any vs Union

It depends a bit on what you want your function to do. If you want your function to take a vector of all Bs or all Cs, then you can write:

function f(x::Union{Vector{B}, Vector{C}})
    ...
end

(note: you can either have a Vector{A} or an Array{A, 1} (they’re the same), but not a Vector{A, 1} (that’s not a thing)).

If you want to mix and match Bs and Cs within the same vector, then you can write:

function f{T <: A}(x::Vector{T})
    ...
end

or, you can use Julia v0.6’s new syntax to write:

function f(x::Vector{<:A})
    ...
end

But note that having a vector that mixes elements of type B and C is going to be slower, since Julia won’t know the exact type of each element of the vector. For more discussion of that situation, check out this thread: How to make a vector of parametric composite types? - #6 by mkborregaard

2 Likes