# Failure in type inference when indexing Tuple with UnitRange (?)

**URL:** https://discourse.julialang.org/t/failure-in-type-inference-when-indexing-tuple-with-unitrange/642
**Category:** General Usage
**Created:** [November 29, 2016, 11:21pm UTC](https://discourse.julialang.org/t/failure-in-type-inference-when-indexing-tuple-with-unitrange/642 "2016-11-29T23:21:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)
#### Post date: [November 29, 2016, 11:21pm UTC](https://discourse.julialang.org/t/failure-in-type-inference-when-indexing-tuple-with-unitrange/642/1 "2016-11-29T23:21:52Z")

</div>

I believe this may be a bug, but thought I’d ask here first before filing an issue:

> julia\> foo{N,T}(bar::NTuple{N,T})=sum(bar[1:N]);@code\_warntype foo((1,))  
> Variables:  
> #self#::#foo  
> bar::Tuple{Int64}

> Body:  
> begin  
> return (Main.sum)((Expr(:invoke, LambdaInfo for getindex(::Tuple{Int64}, ::UnitRange{Int64}), :(Main.getindex), :(bar), :((Expr(:new, UnitRange{Int64}, 1, :((Base.select\_value)((Base.sle\_int)(1,(Expr(:static\_parameter, 1)))::Bool,(Expr(:static\_parameter, 1)),(Base.box)(Int64,(Base.sub\_int)(1,1)))::Int64)))))))::Any  
> end::Any

Annotating the return type doesn’t help:

> julia\> workspace();foo{N,T}(bar::NTuple{N,T})::Int=sum(bar[1:N]);@code\_warntype foo((1,))  
> Variables:  
> #self#::#foo  
> bar::Tuple{Int64}

> Body:  
> begin  
> return (Base.convert)(Main.Int,(Main.sum)((Expr(:invoke, LambdaInfo for getindex(::Tuple{Int64}, ::UnitRange{Int64}), :(Main.getindex), :(bar), :((Expr(:new, UnitRange{Int64}, 1, :((Base.select\_value)((Base.sle\_int)(1,(Expr(:static\_parameter, 1)))::Bool,(Expr(:static\_parameter, 1)),(Base.box)(Int64,(Base.sub\_int)(1,1)))::Int64)))))))::Any)::Any  
> end::Any

Version info:

> julia\> versioninfo()  
> Julia Version 0.5.0  
> Commit 3c9d753 (2016-09-19 18:14 UTC)  
> Platform Info:  
> System: NT (x86\_64-w64-mingw32)  
> CPU: Intel(R) Processor 5Y10 CPU @ 0.80GHz  
> WORD\_SIZE: 64  
> BLAS: libopenblas (USE64BITINT DYNAMIC\_ARCH NO\_AFFINITY Haswell)  
> LAPACK: libopenblas64\_  
> LIBM: libopenlibm  
> LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)

Any suggestions for a workaround? Splatting the tuple into an array first within the function resolves the type inference issue, but generates horrible looking code.

Thanks!  
Simon

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 29, 2016, 11:44pm UTC](https://discourse.julialang.org/t/failure-in-type-inference-when-indexing-tuple-with-unitrange/642/2 "2016-11-29T23:44:24Z")

</div>

If you really need tuple, you generally want to do most computation in type domain. `1:N` puts it into a value so putting it back to a type with `bar[1:N]` won’t be inferrable. (Writing a loop should be inferrable though)

Return type annotation doesn’t work is fixed on master.

---

<div class="post-metadata">

### Author: ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)
#### Post date: [November 29, 2016, 11:53pm UTC](https://discourse.julialang.org/t/failure-in-type-inference-when-indexing-tuple-with-unitrange/642/3 "2016-11-29T23:53:34Z")

</div>

Thanks yuyichao! A loop works fine indeed. Should have thought of that first, must be my Matlab upbringing getting the better of me 😊
