# Type Stable Iteration over Static Parameters

**URL:** <https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380>\
**Category:** General Usage\
**Tags:** question, type-stability\
**Created:** [August 30, 2023, 5:26pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380 "2023-08-30T17:26:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [August 30, 2023, 5:26pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/1 "2023-08-30T17:26:59Z")

</div>

I have a use case where I need to iterate over a set of static parameters. I’m not sure how to do that in a type stable way. A MWE is below:

```julia
function foo(::Val{N}) where N
    n = 0
    for i in 1:N
        n += sum(ntuple(identity, Val(i)))
    end 
    return n
end

```

I’ve tried hacking together something like the following, but it’s still not type stable

```julia
foo(::Val{N}) where N = foo(Val.(ntuple(identity, N))...)

function foo(inds::Vararg{<:Val})
    n = 0
    for i in inds
        n += sum(ntuple(identity, i))
    end 
    return n
end

```

Is there a good way to make something like this type stable?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 30, 2023, 5:33pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/2 "2023-08-30T17:33:05Z")

</div>

As far as I can tell, this should be stable because of constant propagation. How are you measuring type stability here?

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [August 30, 2023, 5:37pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/3 "2023-08-30T17:37:23Z")

</div>

I’m seeing problems with `@code_warntype` as well as `@btime` telling me things are allocating.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 30, 2023, 6:22pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/4 "2023-08-30T18:22:13Z")

</div>

> [@Sukera](#):
>
> As far as I can tell, this should be stable because of constant propagation. How are you measuring type stability here?

But is that how loop compilation works?

I assume it’s not unrolled even though everything is known to the compiler, so each `Val` is still unstable and boxed. `@code_llvm` suggests that’s whats happening.

So @bmit If you want this to compile away, you need to use recursion somehow or other rather than a loop:

```julia
julia> function foo2(::Val{N}) where N
           reduce(ntuple(identity, N); init=0) do acc, i
               acc + sum(ntuple(identity, Val{i}()))
           end
       end

julia> @btime foo2(Val{10}())
  1.072 ns (0 allocations: 0 bytes)
220

```

But this also has type stability limits, and wont compile away for all `N`. If you use `N = 11` it could be a few orders of magnitude slower.

Edit: by specifing this is `foldable` you can make it compile away for all `N`

```julia
Base.@assume_effects foldable function foo2(::Val{N}) where N
   reduce(ntuple(identity, N); init=0) do acc, i
       acc + sum(ntuple(identity, Val{i}()))
   end
end

```

But, if you make `N=1000` compilation may hang.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [August 31, 2023, 6:01am UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/5 "2023-08-31T06:01:39Z")

</div>

An example that more faithfully/realistically represents your problem may help. In particular, is the final result supposed to get completely constant folded in your actual problem?

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [September 1, 2023, 2:48pm UTC](https://discourse.julialang.org/t/type-stable-iteration-over-static-parameters/103380/6 "2023-09-01T14:48:35Z")

</div>

I think I’ve found the issue is actually more with `Generators` and `Iterators.flatten`.

I’ve posted a better MWE [here](https://discourse.julialang.org/t/new-iterator-from-nested-iterators/103377/13) if you’re interested.
