# Static for loops

**URL:** <https://discourse.julialang.org/t/static-for-loops/93051>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 16, 2023, 10:08pm UTC](https://discourse.julialang.org/t/static-for-loops/93051 "2023-01-16T22:08:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)\
**Post date:** [January 16, 2023, 10:08pm UTC](https://discourse.julialang.org/t/static-for-loops/93051/1 "2023-01-16T22:08:30Z")

</div>

I have a function `f(::Val{m}) where m` which I want to specialize to a few values. Can I do it in a for loop instead of writing all the specializations by hand?

I tried

```julia
for m in 1:3
     f(Val(m))
end

```

and

```julia
for m in (1, 2, 3)
     f(Val(m))
end

```

but unfortunately both produce type-unstable code.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [January 16, 2023, 10:19pm UTC](https://discourse.julialang.org/t/static-for-loops/93051/2 "2023-01-16T22:19:14Z")

</div>

How about using `Base.Cartesian.@nexprs`?

---

<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 16, 2023, 10:20pm UTC](https://discourse.julialang.org/t/static-for-loops/93051/3 "2023-01-16T22:20:23Z")

</div>

`ntuple(m -> f(Val(m)), Val(3))` should do the trick, I think?

The compiler should eliminate the tuple construction if it is not used. (Still, it might be nice to have a `foreach` method that accepted a `Val` for static loops instead of using `ntuple` for this kind of thing.)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [January 16, 2023, 10:20pm UTC](https://discourse.julialang.org/t/static-for-loops/93051/4 "2023-01-16T22:20:46Z")

</div>

Don’t have a terminal handy, but `map` works better than loops for stability on tuples. Even better is probably the `ntuple` function. The below matches your MWE and might be unrolled like you want:  
`ntuple(i -> f(Val(i)), Val(3))`

If not, you can write a macro to manually unroll a loop (or some package probably already has it) for a set of values fixed at compile time.

EDIT: the above beat me to it. Also, the suggested `@nexprs` is probably easier than a loop unrolling macro, if you decide to go that way.

---

<div class="post-metadata">

**Author:** ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)\
**Post date:** [January 17, 2023, 4:59am UTC](https://discourse.julialang.org/t/static-for-loops/93051/5 "2023-01-17T04:59:31Z")

</div>

Thank you. I think `@nexprs` is the most simple for my use case.

---

<div class="post-metadata">

**Author:** ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)\
**Post date:** [January 17, 2023, 5:59am UTC](https://discourse.julialang.org/t/static-for-loops/93051/6 "2023-01-17T05:59:37Z")

</div>

Say my function `f` is simply `f(::Val{m}) where m = println(m)`. When I execute the line `ntuple(m -> f(Val(m)), Val(3))` in the REPL I get

```julia
1
2
3
(nothing, nothing, nothing)

```

Do you mean that the tuple `(nothing, nothing, nothing)` will be elided by the compiler if not used?

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [January 17, 2023, 8:49am UTC](https://discourse.julialang.org/t/static-for-loops/93051/7 "2023-01-17T08:49:41Z")

</div>

Hi,  
I am not sure to understand the type/performance issue of a for loop. Would you kindly explain ?

if the type of  
`f(Val(m))`  
is not Int64, is there a performance issue ?

Thanks !

```julia
julia> for m in (1, 2, 3)
           println(typeof(m))
       end
Int64
Int64
Int64

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [January 17, 2023, 3:12pm UTC](https://discourse.julialang.org/t/static-for-loops/93051/8 "2023-01-17T15:12:36Z")

</div>

The `(nothing, nothing, nothing)` is the result of the return value from `println` being run each of 3 times. Yes, the compiler will usually drop these sorts of things if you don’t use them.

That said, this case of `(nothing, nothing, nothing)` has zero size and is type stable (the compiler knows that `println` returns `nothing` and that it’s run 3 times) so it actually _only exists within the compiler_ and the values will never “appear” on your computer’s RAM or processor whether you use them or not.
