I’m trying to understand why type instability appears for certain indexing operations. Suppose I define three functions (MWE for a longer code where type parameter d
is used to index into part of a tuple/array)
struct bar{d} end
foo1(a::bar{d},x) where {d} = x[1:d]
foo2(a::bar{d},x) where {d} = x[2:d+1]
foo3(a::bar{d},x) where {d} = ntuple(i -> x[i+1], d)
If I check if the output type can be inferred, then I get
using Test
@inferred foo1(bar{2}(),(1,2,3,4)) # type stable
@inferred foo2(bar{2}(),(1,2,3,4)) # not type stable
@inferred foo3(bar{2}(),(1,2,3,4)) # type stable
Can someone help me with the logic of why the second one isn’t type stable but the first and last are?