# Type instability with ntuple

**URL:** <https://discourse.julialang.org/t/type-instability-with-ntuple/12699>\
**Category:** New to Julia\
**Tags:** type-stability, ntuple\
**Created:** [July 28, 2018, 9:37am UTC](https://discourse.julialang.org/t/type-instability-with-ntuple/12699 "2018-07-28T09:37:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![retrosnub](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@retrosnub](https://discourse.julialang.org/u/retrosnub)\
**Post date:** [July 28, 2018, 9:37am UTC](https://discourse.julialang.org/t/type-instability-with-ntuple/12699/1 "2018-07-28T09:37:08Z")

</div>

While experimenting with some performance bottlenecks, I found that

```julia
h1(n) = ntuple(i -> collect(0:1/n:1), 3)
@code_warntype h1(5)

```

show type instability (it cannot infer 3 as the length of the tuple), while

```julia
f(n) = ntuple(i -> n, 3)
g(n) = collect(0:1/n:1)
h2(n) = f(g(n))
@code_warntype h2(5)

```

seems to be fine.  
I feel like this is non-expected behaviour and if possible I’d like `h1` to be type stable without the need to split it up like that. Is this a known issue or am I just doing things the wrong way?

---

<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:** [July 28, 2018, 11:51am UTC](https://discourse.julialang.org/t/type-instability-with-ntuple/12699/2 "2018-07-28T11:51:10Z")

</div>

> [@retrosnub](#):
>
> I feel like this is non-expected behaviour and if possible I’d like `h1` to be type stable without the need to split it up like that. Is this a known issue or am I just doing things the wrong way?

On 0.6 both versions are type-unstable; presumably on 0.7 the second version is simple enough to benefit from the new constant-propagation optimization.

Anyway, you can pass `Val{3}()` if you want to force the length `3` to be a compile-time constant.

PS. Your `h1` and `h2` are inequivalent in another way: `h1` has a _different_ `collected` array in each tuple element, whereas `h2` has a tuple of three references to the _same_ array.

---

<div class="post-metadata">

**Author:** ![retrosnub](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@retrosnub](https://discourse.julialang.org/u/retrosnub)\
**Post date:** [July 28, 2018, 1:50pm UTC](https://discourse.julialang.org/t/type-instability-with-ntuple/12699/3 "2018-07-28T13:50:08Z")

</div>

Sorry for not mentioning, I’m on 0.7 master right now.  
Thanks! I didn’t even know you could pass `Val{n}()` to `ntuple`, that is nice.  
There might be other calls to `Base`-functions in my code, where I can pass `Val{n}()`s to enforce type stability.

Yes, `h1` and `h2` are indeed different in that sense, I didn’t notice when I put together the example.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 28, 2018, 2:01pm UTC](https://discourse.julialang.org/t/type-instability-with-ntuple/12699/4 "2018-07-28T14:01:45Z")

</div>

Note that you can write Val(3) instead. EDIT: On Julia 0.7
