# Feature request: variant of -\> that uses current bindings

**URL:** <https://discourse.julialang.org/t/feature-request-variant-of-that-uses-current-bindings/118161>\
**Category:** General Usage\
**Created:** [August 14, 2024, 1:06am UTC](https://discourse.julialang.org/t/feature-request-variant-of-that-uses-current-bindings/118161 "2024-08-14T01:06:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 14, 2024, 1:06am UTC](https://discourse.julialang.org/t/feature-request-variant-of-that-uses-current-bindings/118161/1 "2024-08-14T01:06:09Z")

</div>

It is a documented feature of Julia (see [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured)) that using a variable from the main body of a function in an inner function causes the inner and outer copy of the variable to be bound together. Although this provides the programmer with additional flexibility in some situations, it is often not needed and causes a performance loss. As mentioned in the manual, if the inner function needs only the current binding of the outer variable, then the programmer indicates this with `let` statements. I would like to request a variant of `->`, say `-->`, that automatically uses current bindings of all outer variables and thus frees the programmer from writing many `let` statements.

E.g., in the following code, I’d like to be able to write `fn2 = x --> q*x` with the same effect as the actual definition of `fn2` below.

```julia
function testbindings()
    p = 8
    fn = x -> x*p
    println(fn(2))
    p = 5
    println(fn(2))
    q = 5
    fn2 = let q = q
        x -> q * x
    end
    println(fn2(2))
    q = 10
    println(fn2(2))
    nothing
end

```

which yields output 16, 10, 10, 10

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [August 14, 2024, 3:26am UTC](https://discourse.julialang.org/t/feature-request-variant-of-that-uses-current-bindings/118161/2 "2024-08-14T03:26:08Z")

</div>

There’s [GitHub - c42f/FastClosures.jl: Faster closure variable capture](https://github.com/c42f/FastClosures.jl) which does this (with a different syntax)

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 14, 2024, 10:34pm UTC](https://discourse.julialang.org/t/feature-request-variant-of-that-uses-current-bindings/118161/3 "2024-08-14T22:34:43Z")

</div>

I agree that FastClosures.jl does what I requested. However, the source code for FastClosures.jl has many comments expressing reservations that the code may not always work, etc., because it tries to simulate the compiler as it macro-expands without actually invoking the compiler. Therefore, it would be better if this functionality could be built into the compiler itself.
