i was trying to generate an array of two types, let’s say A and B, and just wanted to make sure my arguments to the functions handling this vector are explicitly typed.
However i realized that i’m not solving my problem correctly.
what i should really be doing, I think, is something like
abstract A
type B <: A end
type C <: A end
Vector{A,1}
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
While every application will be different, for me when there is no clear reason to give a Vector a particular abstract eltype, I fall back to Vector{Any} for performance reasons: this avoids overspecialization at the expense of less type information for the compiler, but that is often not a concern when the vector is small and will be used only a few times, as the case might often be.