# Array of type Any vs Union

**URL:** <https://discourse.julialang.org/t/array-of-type-any-vs-union/2788>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 21, 2017, 5:47am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788 "2017-03-21T05:47:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [March 21, 2017, 5:47am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/1 "2017-03-21T05:47:39Z")

</div>

```julia
julia> ["s", 1]
2-element Array{Any,1}:
  "s"
 1   

julia> ["s", 1] :: Array{Union{String, Int},1}
ERROR: TypeError: typeassert: expected Array{Union{Int64,String},1}, got Array{Any,1}

```

Im thinking at this point that I probably have to promote the “s” and 1 as actual elements of a specific Union type but I can’t make it work.

```julia
julia> foo = Union{Int, String}
Union{Int64,String}

julia> ["s", 1]
2-element Array{Any,1}:
  "s"
 1   

julia> ["s"::foo, 1::foo]
2-element Array{Any,1}:
  "s"
 1   

julia> ["s"::foo, 1::foo] :: Array{foo,1}
ERROR: TypeError: typeassert: expected Array{Union{Int64,String},1}, got Array{Any,1}

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 21, 2017, 5:56am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/2 "2017-03-21T05:56:57Z")

</div>

If you really want this, you can construct the `Array{Union{String, Int}}` directly:

```julia
julia> Union{String, Int}["s", 1]
2-element Array{Union{Int64,String},1}:
  "s"
 1

```

or

```julia
julia> convert(Vector{Union{String, Int}}, ["s", 1])
2-element Array{Union{Int64,String},1}:
  "s"
 1

```

But can I ask what you need this for? I think it’s pretty unusual to explicitly declare a `Vector{Union...}`.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [March 22, 2017, 3:44am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/3 "2017-03-22T03:44:06Z")

</div>

Hi,  
Thanks for that.

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

```julia
abstract A
type B <: A end
type C <: A end

Vector{A,1}

```

Right ?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 22, 2017, 4:28am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/4 "2017-03-22T04:28:28Z")

</div>

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:

```julia
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 `B`s and `C`s within the same vector, then you can write:

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

```

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

```julia
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](https://discourse.julialang.org/t/how-to-make-a-vector-of-parametric-composite-types/2439/6)

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [March 22, 2017, 4:34am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/5 "2017-03-22T04:34:28Z")

</div>

Yes, the idea is that I have both B & C types in the vector.

This is actually an intermediate step.

The source vector is mixed, and in the next step the vector is separated into two vectors of B and C.

I’m thinking about the original problem a little more ,because I’m still thinking that maybe I’m not solving it in the best way.

Meanwhile I sort of took a detour into why I couldn’t seem to create a vector of a Union.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 22, 2017, 5:38am UTC](https://discourse.julialang.org/t/array-of-type-any-vs-union/2788/6 "2017-03-22T05:38:59Z")

</div>

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.
