# How to use @ntuple with N derived from parameters

**URL:** <https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792>\
**Category:** General Usage\
**Tags:** ntuple\
**Created:** [June 24, 2023, 9:11pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792 "2023-06-24T21:11:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [June 24, 2023, 9:11pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792/1 "2023-06-24T21:11:36Z")

</div>

Below is a MWE of an error I get using `@ntuple`:

```julia
using StaticArrays
import Base.Cartesian: @ntuple

function times2(v::SVector{N, T}) where {N, T}
    return SVector{N, T}(@ntuple N i->v[i]*2)
end

a = randn(SVector{3, Float64})
b = times2(a)

```

Does anyone know what I need to change to get this code to run? I’ve tried several things already, but always get the same error:

```julia
ERROR: LoadError: MethodError: no method matching var"@ntuple"(::LineNumberNode, ::Module, ::Symbol, ::Expr)

Closest candidates are:
  var"@ntuple"(::LineNumberNode, ::Module, ::Int64, ::Any)
   @ Base cartesian.jl:199

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 24, 2023, 9:12pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792/2 "2023-06-24T21:12:08Z")

</div>

`@ntuple` only works when `N` is a literal number, not a variable. Instead write

```julia
function times2(v::SVector{N, T}) where {N, T}
    return SVector{N, T}(ntuple(i->v[i]*2, N))
end

```

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [June 24, 2023, 9:14pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792/3 "2023-06-24T21:14:17Z")

</div>

I see. Is there a reason why it cannot work with a compile-time constant such as the `N` in my code? Or is it something that could be fixed?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 24, 2023, 9:15pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792/4 "2023-06-24T21:15:25Z")

</div>

Macros operate just on parsed syntax, they don’t know about compile time constants.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [June 24, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792/5 "2023-06-24T21:20:56Z")

</div>

Makes sense.

> [@Mason](#):
>
> ```julia
> function times2(v::SVector{N, T}) where {N, T}
> return SVector{N, T}(ntuple(i->v[i]*2, N))
> end
> 
> ```

Unfortunately this kills performance. However, the following works well:

```julia
function times2(v::SVector{N, T}) where {N, T}
    return SVector{N, T}(ntuple(i->v[i]*2, Val(N)))
end

```
