# Strange re-typing of arrays of numbers

**URL:** <https://discourse.julialang.org/t/strange-re-typing-of-arrays-of-numbers/51603>\
**Category:** New to Julia\
**Tags:** question, type, dispatch\
**Created:** [December 10, 2020, 12:55pm UTC](https://discourse.julialang.org/t/strange-re-typing-of-arrays-of-numbers/51603 "2020-12-10T12:55:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xyz1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xyz1/32/19984_2.png) [@xyz1](https://discourse.julialang.org/u/xyz1)\
**Post date:** [December 10, 2020, 12:55pm UTC](https://discourse.julialang.org/t/strange-re-typing-of-arrays-of-numbers/51603/1 "2020-12-10T12:55:59Z")

</div>

Hello, I was putting together some multiple-dispatches to find that it doesn’t work as I would expect. Or rather, maybe dispatching works perfectly great, but my arrays of numbers get re-typed strangely.  
When you have a list of lists of which all but one are integer lists:

```julia
x = [[2,3,4], [2,5], []]

```

it correctly assumes the type:  
Array{Array{Any,1},1}

But if you afterwards try to extract the type of the first sub-list like so:

```julia
typeof(x[1])

```

It is surprisingly (for me) not a list of integers (but it is), but it is Array{Any,1}.

Could someone explain to me what is happening in the background? I wanted to use a parametric multiple dispatch that would handle list of sublists of the same type differently than a list of sublists of different types. So why doesn’t the type above get re-typed to list of integers? My multi-dispatch with which I discovered this behavior:

```julia
function(a::Vector{T}, b::Vector{T}) where {T}
    ..
end

function(a::Vector{T}, b::Vector{U}) where {T, U}
    ..
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 10, 2020, 1:11pm UTC](https://discourse.julialang.org/t/strange-re-typing-of-arrays-of-numbers/51603/2 "2020-12-10T13:11:15Z")

</div>

> [@xyz1](#):
>
> `x = [[2,3,4], [2,5], []]`

`[]` is a `Vector{Any}`, and literal array construction defaults to promoting all elements to the same type. Compare:

```julia
julia> x = [[2,3,4], [2,5], []]
3-element Array{Array{Any,1},1}:
 [2, 3, 4]
 [2, 5]
 []

julia> x = [[2,3,4], [2,5], Int[]]
3-element Array{Array{Int64,1},1}:
 [2, 3, 4]
 [2, 5]
 []

```

I think the latter is what you want.

If you really need the elements of the array to have different types, you need to declare the container to have an abstract type like `Any`:

```julia
julia> x = Any[[2,3,4], [2,5], []]
3-element Array{Any,1}:
 [2, 3, 4]
 [2, 5]
 Any[]

julia> x[1]
3-element Array{Int64,1}:
 2
 3
 4

```

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 10, 2020, 1:51pm UTC](https://discourse.julialang.org/t/strange-re-typing-of-arrays-of-numbers/51603/3 "2020-12-10T13:51:48Z")

</div>

This happened twice now in a short time. Compare:

> [@Types, strange behavior](https://discourse.julialang.org/t/types-strange-behavior/51384):
>
> Julia Version 1.5.3 (2020-11-09) Hi, looking at this vector=[[1,2],[3,[4,5]]] vector[1] typeof(vector[1]) julia\> vector=[[1,2],[3,[4,5]]] 2-element Array{Array{Any,1},1}: [1, 2] [3, [4, 5]] julia\> vector[1] 2-element Array{Any,1}: 1 2 julia\> typeof(vector[1]) Array{Any,1} Is there a good reason Julia cannot define the type to be Vector{Int64}?

Should there be a warning in the docs? Like, for example, here:

[https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals](https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals)

Something along the lines of:

"Remember, promotion to a common type may also change the type of nested arrays:

```julia
julia> arr = [Int[1,2],[]]
3-element Array{Array{Any,1},1}:
 [1, 2]
 []

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

```

To prevent this, declare an abstract type for the containing array:

```julia
julia> arr2 = Any[[1,2],[]]
3-element Array{Any,1}:
 [1, 2]
 Any[]

julia> arr2[1]
2-element Array{Int64,1}:
 1
 2

```

"
