How to capture an index in closure without allocating (or workaround)?

Remembering that “Closures are poor man’s objects and objects are poor man’s closures” you can explicitly represent and pass in your closure:

mutable struct MyInner{T1, T2}
    i::T1
    res::T2
end

function (in::MyInner)(c1, c2)
    allCombi = Iterators.flatten(Iterators.product(c1, c2))
    for combi in allCombi
        in.i += 1
        in.res[in.i] = combi[1] > 5.0 ? nothing : 2.0
    end
    return
end

function testIt!(inner, v1, v2)
    function outer(c1)
        itrCombis(c2 -> inner(c1, c2), v2, Val(4))
        return
    end
    
    itrCombis(outer, v1, Val(4))
    return
end

function run()
    res = Vector{Union{Nothing,Float64}}(undef, 100000000)
    num = 5
    v1 = collect(1:num)
    v2 = collect(1:num)
    in = MyInner(0, res)
    testIt!(in, v1, v2)
    @time testIt!(in, v1, v2)
end

Don’t think its worth the effort, just to prevent 16 bytes of allocation.