# Help eliminate a captured variable

**URL:** https://discourse.julialang.org/t/help-eliminate-a-captured-variable/136585
**Category:** Performance
**Tags:** question
**Created:** [April 6, 2026, 10:38am UTC](https://discourse.julialang.org/t/help-eliminate-a-captured-variable/136585 "2026-04-06T10:38:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 6, 2026, 10:38am UTC](https://discourse.julialang.org/t/help-eliminate-a-captured-variable/136585/1 "2026-04-06T10:38:00Z")

</div>

I am trying to eliminate a captured variable, as reported by JET.jl, in [this piece of code](https://github.com/tpapp/TransformVariables.jl/blob/6357270d30e1efd628c31ee2e81e0c7cf6584583/src/aggregation.jl#L202), copied here for convenience:

```julia
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
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?

---

<div class="post-metadata">

### Author: ![aviatesk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aviatesk/32/7610_2.png) [@aviatesk](https://discourse.julialang.org/u/aviatesk)
#### Post date: [April 6, 2026, 10:51am UTC](https://discourse.julialang.org/t/help-eliminate-a-captured-variable/136585/2 "2026-04-06T10:51:08Z")

</div>

The `let` approach doesn’t work if you assign the variables within closures. I’d recommend using mutable container to avoid direct assignments in such cases:  
[https://aviatesk.github.io/JETLS.jl/release/diagnostic/#Workaround-d9291b076db1c42c](https://aviatesk.github.io/JETLS.jl/release/diagnostic/#Workaround-d9291b076db1c42c)
