As you can tell, it took a few tries ![]()
function test4(tup,idx,typ::Type{T}) where T
idx == 1 && (x=first(tup); isa(x,T) && return x)
test4(Base.tail(tup),idx-1,T)
end
This seems to do the trick and not allocate (throws an error otherwise), it is also type stable. outer_test would not allocate regardless of inlining.
Hope this helps…
Edit:
This seems to do the same and simpler:
function test4(tup,idx,typ::Type{T}) where T
idx == 1 && (x=first(tup); return x::T)
#or if you want an explicit error
#isa(x,T) ? (return x) : error("Bla"))
test4(Base.tail(tup),idx-1,T)
end