# How to declare N nested Vectors?

**URL:** <https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813>\
**Category:** General Usage\
**Tags:** arrays, vector-of-vectors\
**Created:** [February 27, 2024, 12:14am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813 "2024-02-27T00:14:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaojin](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@chaojin](https://discourse.julialang.org/u/chaojin)\
**Post date:** [February 27, 2024, 12:14am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/1 "2024-02-27T00:14:03Z")

</div>

HI,  
I want to declare N nested Vectors in Julia.  
e.g.

```julia
Vector{Int}
Vector{Vector{Int}}
...
Vector{Vector{....Vector{Int}...} # N dimensional nested Vector

```

In C++, it can be implemented by using recursive specialization template like below (please refer to [c++ - n-dimensional vector - Stack Overflow](https://stackoverflow.com/questions/13105514/n-dimensional-vector))

```julia
template<size_t dimcount, typename T>
struct multidimensional_vector
{
    typedef std::vector< typename multidimensional_vector<dimcount-1, T>::type > type;
};

template<typename T>
struct multidimensional_vector<0,T>
{
    typedef T type;
};

multidimensional_vector<1, int>::type v;
multidimensional_vector<2, int>::type v2;
multidimensional_vector<3, int>::type v3;
multidimensional_vector<4, int>::type v4;

```

Is there a similar method in Julia to define N dimensional Vectors by recursive specialized parameters N.

Thanks.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 27, 2024, 12:20am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/2 "2024-02-27T00:20:29Z")

</div>

Julia has native N dimensional arrays. `Vector{Int}` is just `Array{Int,1}`, `Matrix{Int}` is `Array{Int,2}`, but Julia support up to 511 dimensions.

---

<div class="post-metadata">

**Author:** ![chaojin](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@chaojin](https://discourse.julialang.org/u/chaojin)\
**Post date:** [February 27, 2024, 1:27am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/3 "2024-02-27T01:27:52Z")

</div>

Thanks Oscar. N dimensional array is one option. But difference is that array has the same size of each row, but vector is more dynamic. In my application, that is exactly different size with different row, like:  
row 1: 1,2,3  
row 2: 3,4,5,6,…, 20  
…, etc.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 27, 2024, 6:40am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/4 "2024-02-27T06:40:55Z")

</div>

In terms of terminology, that is not an N-dimensional array, but simply nested vectors. And Julia already have those. Perhaps you can explain what you mean be “implementing them”, seeing as they already exist?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 27, 2024, 6:59am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/5 "2024-02-27T06:59:47Z")

</div>

You could use a macro to do this.

```julia-repl
julia> macro NVector(N,T)
           e = T
           for i in 1:N
               e = :(Vector{$e})
           end
           return e
       end
@NVector (macro with 1 method)

julia> @NVector(0,Int)
Int64

julia> @NVector(1,Int)
Vector{Int64} (alias for Array{Int64, 1})

julia> @NVector(2,Int)
Vector{Vector{Int64}} (alias for Array{Array{Int64, 1}, 1})

julia> @NVector(3,Int)
Vector{Vector{Vector{Int64}}} (alias for Array{Array{Array{Int64, 1}, 1}, 1})

julia> @NVector(4,Int)
Vector{Vector{Vector{Vector{Int64}}}} (alias for Array{Array{Array{Array{Int64, 1}, 1}, 1}, 1})

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 27, 2024, 7:12am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/6 "2024-02-27T07:12:34Z")

</div>

See also

> **[Home · ArraysOfArrays](https://juliaarrays.github.io/ArraysOfArrays.jl/dev/)**
>
> Documentation for ArraysOfArrays.

> **[GitHub - JuliaHEP/AwkwardArray.jl: Awkward Array in Julia mirrors the Python...](https://github.com/JuliaHEP/AwkwardArray.jl)**
>
> Awkward Array in Julia mirrors the Python library, enabling effortless zero-copy data exchange between Julia and Python - JuliaHEP/AwkwardArray.jl

---

<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:** [February 27, 2024, 7:14am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/7 "2024-02-27T07:14:51Z")

</div>

The type may be constructed without macros as well

```julia
julia> foldl((T,_) -> Vector{T}, 1:4, init=Int)
Vector{Vector{Vector{Vector{Int64}}}} (alias for Array{Array{Array{Array{Int64, 1}, 1}, 1}, 1})

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 27, 2024, 7:51am UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/8 "2024-02-27T07:51:46Z")

</div>

Based on some of the answers here, it seems you are not really looking for how to _implement_ the type, but perhaps for a convenient way to _declare_ it? Is that right? Maybe you can update the thread title and OP?

---

<div class="post-metadata">

**Author:** ![chaojin](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@chaojin](https://discourse.julialang.org/u/chaojin)\
**Post date:** [February 27, 2024, 2:54pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/9 "2024-02-27T14:54:47Z")

</div>

Yes my question is how to declare N nested Vector. I have updated thread title.

Thanks mkitti and jishnub. That is exactly addressing my question.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 27, 2024, 3:02pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/10 "2024-02-27T15:02:51Z")

</div>

Julia’s methods are themselves very much like C++'s templates. You can convert your recursive code to Julia in a 1:1 manner, too:

```Julia
julia> nvec(::Val{n}, ::Type{T}) where {dimcount, T} = nvec(Val(dimcount-1), Vector{T})
nvec (generic function with 2 methods)

julia> nvec(::Val{0}, ::Type{T}) where {T} = T
nvec (generic function with 2 methods)

julia> nvec(Val(3), Int)
Vector{Vector{Vector{Int64}}} (alias for Array{Array{Array{Int64, 1}, 1}, 1})

```

The special `Val` type allows you to dispatch on the value of `dimcount`.

---

<div class="post-metadata">

**Author:** ![chaojin](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@chaojin](https://discourse.julialang.org/u/chaojin)\
**Post date:** [February 27, 2024, 3:25pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/11 "2024-02-27T15:25:11Z")

</div>

I have tried parametric Type with Val as such: struct NDVector{::Val{N}} and hit error.  
Your method is using parametric method. Yes that is 1:1 manner with C++.  
Thank you @mbauman

---

<div class="post-metadata">

**Author:** ![NumHack](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@NumHack](https://discourse.julialang.org/u/NumHack)\
**Post date:** [February 27, 2024, 3:30pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/12 "2024-02-27T15:30:23Z")

</div>

I’ve been looking for such a Julia equivalent of C++ recursive parametric templates for a while, thanks a lot !

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 6:55pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/13 "2024-02-27T18:55:29Z")

</div>

> [@Oscar\_Smith](#):
>
> Julia has native N dimensional arrays. `Vector{Int}` is just `Array{Int,1}`, `Matrix{Int}` is `Array{Int,2}`, but Julia support up to 511 dimensions.

Out of curiosity, is this only a pragmatic limit, as in a reasonable constraint for how many cases the compiler has to deal with, or is this also being stored as a nine-bit value in a tag somewhere?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 27, 2024, 7:11pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/14 "2024-02-27T19:11:17Z")

</div>

[9 bits](https://github.com/JuliaLang/julia/blob/997b49fbf491f99da8f0c09aeab64400f84a0e47/src/julia.h#L179). In Julia 1.11, though the number is bumped up to `typemax(Int)`. (although actually constructing one with more than a couple thousand dimensions will likely crash the compiler)

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [February 27, 2024, 7:13pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/15 "2024-02-27T19:13:14Z")

</div>

At least printing starts to break at 491 dimensions in 1.10

```julia
julia> Array{Int, 491}(undef, ntuple(i -> 1, 491))

(...trimmed...)

Internal error: stack overflow in type inference of view(Array{Int64, 491}, Base.UnitRange{Int64}, Base.UnitRange{Int64}, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64, Int64).
This might be caused by recursion over very long tuples or argument lists.

```

And it breaks at 429 on 1.12 nightly

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 27, 2024, 8:14pm UTC](https://discourse.julialang.org/t/how-to-declare-n-nested-vectors/110813/16 "2024-02-27T20:14:47Z")

</div>

> [@BioTurboNick](#):
>
> At least printing starts to break at 491 dimensions in 1.10

The printing issue also occurs for [deeply recursive types](https://discourse.julialang.org/t/julia-never-stops-compiling-for-new-recursive-vector-types/110232/19), as I discovered recently.

Deeply nested containers kind of have to overflow the stack, because it’s too late to add a depth counter to `show`, which at least the internal implementations could have used.

But type printing is internal\*, and could use a stack with a loop to avoid this. Might be a good first issue, since the fix would be fairly self-contained.

[\*] this isn’t 100% true, since it’s possible to overload `show(::IO, ::Type{MyType})`, however bad an idea that might be. But handling it for the builtin types would flatten the stack enough to avoid a blowup in many more cases.
