Actually based on the output of @code_warntype
using a for
loop does still yield type instability because the type of f
is different on each iteration.
Replacing the for loop with individual indexing:
s += b.foos[1].a
s += b.foos[2].a
yields completely type stable code. Look at this interesting thread for interesting general approaches that don’t involve hardcoding Type stability while looping over Tuple. Namely recursion and the Unrolled.jl package