I am trying to eliminate a captured variable, as reported by JET.jl, in this piece of code, copied here for convenience:
function transform_with(flag::LogJacFlag, transformation::StaticArrayTransformation{D,S},
x::AbstractVector{T}, index::Int) where {D,S,T}
(; inner_transformation) = transformation
# NOTE this is a fix for #112, enforcing types taken from the transformation of the
# first element.
y1, β1, index1 = transform_with(flag, inner_transformation, x, index)
D == 1 && return SArray{S}(y1), β1, index1
L = typeof(β1)
let β::L = β1, index::Int = index1
function _f(_)
y, βΞ, indexβ² = transform_with(flag, inner_transformation, x, index)
index = indexβ²
β = β + βΞ
y
end
yrest = SVector{D-1}(_f(i) for i in 2:D)
SArray{S}(pushfirst(yrest, y1)), β, index
end
end
The reproducer is below, on Julia 1.12.5, JET.jl reports
julia> using TransformVariables, JET, StaticArrays
julia> t = as(SVector{3}, asββ)
TransformVariables.StaticArrayTransformation{3, Tuple{3}, TVExp}(asββ) (dimension 3)
julia> @report_opt transform(t, ones(3))
βββββ 2 possible errors found βββββ
β transform(t::TransformVariables.StaticArrayTransformation{3, Tuple{3}, TVExp}, x::Vector{Float64}) @ TransformVariables /home/tamas/code/julia/TransformVariables/src/generic.jl:315
ββ transform_with(flag::TransformVariables.NoLogJac, transformation::TransformVariables.StaticArrayTransformation{β¦}, x::Vector{β¦}, index::Int64) @ TransformVariables /home/tamas/code/julia/TransformVariables/src/aggregation.jl:210
ββ captured variable `β` detected
ββββββββββββββββββββββ
ββ transform_with(flag::TransformVariables.NoLogJac, transformation::TransformVariables.StaticArrayTransformation{β¦}, x::Vector{β¦}, index::Int64) @ TransformVariables /home/tamas/code/julia/TransformVariables/src/aggregation.jl:210
ββ captured variable `index` detected
ββββββββββββββββββββββ
I thought that the let would take care of this, what am I missing? Whatβs the recommended fix?