# Why are irregularly shaped arrays of arrays of type Any in Julia?

**URL:** <https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 19, 2021, 5:41pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339 "2021-12-19T17:41:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabriben/32/32040_2.png) [@gabriben](https://discourse.julialang.org/u/gabriben)\
**Post date:** [December 19, 2021, 5:41pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/1 "2021-12-19T17:41:28Z")

</div>

Hi, this is a bit of an edge case coming from trying something out with yesterday’s adventOfCode challenge (day 18).

Why are irregularly shaped arrays of arrays of type `Any`?

For example:

```julia
julia> [[2, 9], 3]
2-element Vector{Any}:
  [2, 9]
 3

```

But as long as I keep the nested arrays are of the same size, the type remains Int:

```julia
julia> [[2, 9], [3, 4]]
2-element Vector{Vector{Int64}}:
 [2, 9]
 [3, 4]

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 19, 2021, 5:42pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/2 "2021-12-19T17:42:57Z")

</div>

Not exactly, because e.g.:

```julia
julia> [[2, 9], [3]]
2-element Vector{Vector{Int64}}:
 [2, 9]
 [3]

```

Your first example is not an array of arrays, it’s an array with one element Array and another element Int.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 19, 2021, 5:43pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/3 "2021-12-19T17:43:23Z")

</div>

Ah, and welcome here!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 19, 2021, 6:12pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/4 "2021-12-19T18:12:26Z")

</div>

Probably the above is what you want, but if not you could do:

```julia
julia> Union{Vector{Int},Int}[[2,9], 3 ]
2-element Vector{Union{Int64, Vector{Int64}}}:
  [2, 9]
 3

julia>

```

---

<div class="post-metadata">

**Author:** ![gabriben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabriben/32/32040_2.png) [@gabriben](https://discourse.julialang.org/u/gabriben)\
**Post date:** [December 19, 2021, 6:42pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/5 "2021-12-19T18:42:26Z")

</div>

The advent of code challenge also has more complicated sequences to be read from a text file. I was curious if it was possible to read it directly into well structured nested arrays. Ideally, I would have liked Julia to figure out `Union{Vector{Int},Int}` from @lmiq automatically, such that it would work for any complicated structure like bellow (full input [here](https://adventofcode.com/2021/day/18/input)):

```julia
[[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]

```

I omitted to mention that I read the file like this:

```julia
d = open("18.txt") do f readlines(f) end
d = eval.(Meta.parse.(d))

```

I am not sure this is a relevant question, because these adventofcode challenges can be a bit far from real use cases anyways and also because this challenge can also be solved by reading the structures as strings and count brackets, as opposed to running `eval`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 19, 2021, 8:36pm UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/6 "2021-12-19T20:36:38Z")

</div>

It doesn’t really matter much since as soon as you have to represent everything as pointers to boxed objects, you’re already hosed in performance anyway. What the problem with the arrays being Any typed?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [December 20, 2021, 3:34am UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/7 "2021-12-20T03:34:39Z")

</div>

Wouldn’t union splitting help with subsequent code while indexing into the array, if the type is known to be a small `Union` instead of `Any`? Or is that not a factor?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 20, 2021, 8:29am UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/8 "2021-12-20T08:29:40Z")

</div>

You can do this to get a fully typed object:

```julia
julia> pairify(x::Int) = x
pairify (generic function with 1 method)

julia> pairify((a, b)::Vector) = pairify(a) => pairify(b)
pairify (generic function with 2 methods)

julia> number = pairify(eval(Meta.parse("[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]")))
((0 => (5 => 8)) => ((1 => 7) => (9 => 6))) => ((4 => (1 => 2)) => ((1 => 4) => 2))

julia> typeof(number)
Pair{Pair{Pair{Int64, Pair{Int64, Int64}}, Pair{Pair{Int64, Int64}, Pair{Int64, Int64}}}, Pair{Pair{Int64, Pair{Int64, Int64}}, Pair{Pair{Int64, Int64}, Int64}}}

```

Sadly you will find that when you try to solve the rest of the problem, compile time will absolutely explode due to all the type specializations you run into. The `@nospecialize` macro might help but I wasn’t able to get reasonable performance with this approach before I changed track.

`Vector{Any}` isn’t that bad an idea.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 20, 2021, 10:42am UTC](https://discourse.julialang.org/t/why-are-irregularly-shaped-arrays-of-arrays-of-type-any-in-julia/73339/9 "2021-12-20T10:42:12Z")

</div>

It might but only in the simple cases. These deeply nested examples are too complex for the compiler to make heads or tails of.
