# @nloops with type parameter N

**URL:** <https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733>\
**Category:** General Usage\
**Tags:** metaprogramming, arrays\
**Created:** [January 2, 2021, 11:28am UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733 "2021-01-02T11:28:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [January 2, 2021, 11:28am UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733/1 "2021-01-02T11:28:29Z")

</div>

Hey,

I was trying to create a function body which contains `@nloops N i A`. However, the `N` depends on the dimension of the array. Following MWE:

```julia
A = randn((2,2,2))
function foo(A::AbstractArray{T, N}) where {T, N}
           @nloops N i A begin
                  println("Do something with the indices")
           end
end

foo(A)

```

However, this throws an error:

```julia
ERROR: LoadError: MethodError: no method matching _nloops(::Symbol, ::Symbol, ::Symbol, ::Expr)
Closest candidates are:
  _nloops(::Int64, ::Symbol, ::Symbol, ::Expr...) at cartesian.jl:42
  _nloops(::Int64, ::Symbol, ::Expr, ::Expr...) at cartesian.jl:47
Stacktrace:
 [1] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
in expression starting at REPL[80]:2

```

I understand this error if the value of `N` would be only available during runtime. However, `N` is already determined by type so I would have expected that it could conceptually work.

How can I solve this problem?

Thanks,

Felix

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 2, 2021, 11:43am UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733/2 "2021-01-02T11:43:17Z")

</div>

The `@nloops` macro only accepts literal integers for `N`. Personally, I think it would make sense to just deprecate the `@nloops` macro, since there are usually better ways to achieve the same thing. It’s difficult to say what the best solution here would be without a more realistic example, but I would highly recommend reading @tim.holy’s great [blog post on multidimensional iteration](https://julialang.org/blog/2016/02/iteration/). `CartesianIndices` may well be what you are looking for here.

---

<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:** [January 2, 2021, 2:24pm UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733/3 "2021-01-02T14:24:31Z")

</div>

> [@roflmaostc](#):
>
> How can I solve this problem?

You need to use a `@generated function foo` and then interpolate `$N` into the returned expression, like the [example in the manual](https://docs.julialang.org/en/v1/devdocs/cartesian/#Supplying-the-number-of-expressions).

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [January 2, 2021, 3:09pm UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733/4 "2021-01-02T15:09:07Z")

</div>

It seems like that I missed this one completely!  
Thanks!

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [January 4, 2021, 12:19pm UTC](https://discourse.julialang.org/t/nloops-with-type-parameter-n/52733/5 "2021-01-04T12:19:47Z")

</div>

Just for completeness, since I wanted to modify `N`:

```julia
julia> using Base.Cartesian

julia> @generated function foo(A::AbstractArray{T, N}) where {T, N}
           quote 
               @nloops $N i A begin
                   println("Do something with the indices")
               end 
           end 
       end
foo (generic function with 1 method)

julia> @generated function foo2(A::AbstractArray{T, N}) where {T, N}
           N2 = N - 1 
           quote 
               @nloops $N2 i A begin
                   println("Do something with the indices")
               end 
           end 
       end
foo2 (generic function with 1 method)

julia> foo(randn((3,3)))
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices
Do something with the indices

julia> foo2(randn((3,3)))
Do something with the indices
Do something with the indices
Do something with the indices

```
