# Whitelist/Blacklist for captured variable

**URL:** <https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714>\
**Category:** Performance\
**Created:** [October 1, 2018, 2:19am UTC](https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714 "2018-10-01T02:19:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [October 1, 2018, 2:19am UTC](https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714/1 "2018-10-01T02:19:30Z")

</div>

While doing `do` block, I pay attention to the variables captured by the closure. I tried some example in the [manual](https://docs.julialang.org/en/latest/manual/performance-tips/#man-performance-captured-1) and the previous discussions (like [recent thread](https://discourse.julialang.org/t/this-should-be-documented-in-performance-tips-if-not-a-temporary-bug/11894) ) and found no `let` needed in some cases. As I don’t know how long it takes to solve this issue completely, I was wondering if a whitelist/blacklist were helpful in current situation (it may seem trivial for advanced user I guess).  
A **whitelist** (i.e., no additional coding like the `let` tip needed)

- to capture the parameter in the enclosing scope of `do` block like

```julia
function outerfunc(r::T)
... ... # no new assignment for r
... do x
    innerfunc(x,r)
    end
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 1, 2018, 12:10pm UTC](https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714/2 "2018-10-01T12:10:52Z")

</div>

This seems like a great use case for writing a macro. It should be possible to write a macro that takes a block like

```julia
f(x, y)
g(x)

```

into

```julia
let f=f, x=x, y=y, g=g
  f(x, y)
  g(x)
end

```

which would do what (I think) you’re looking for.

In particular, the `postwalk` function from MacroTools.jl should make it much easier to walk through the whole expression while you gather up all the Symbols that need to be added to the `let` block.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [October 1, 2018, 12:22pm UTC](https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714/3 "2018-10-01T12:22:48Z")

</div>

That looks a lot like the macro in [https://github.com/c42f/FastClosures.jl](https://github.com/c42f/FastClosures.jl)

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [October 1, 2018, 1:21pm UTC](https://discourse.julialang.org/t/whitelist-blacklist-for-captured-variable/15714/4 "2018-10-01T13:21:07Z")

</div>

Really appreciate all the replies. I noticed that FastClosures is mentioned in the [manual](https://docs.julialang.org/en/latest/manual/performance-tips/#man-performance-captured-1). However, in the most simple use case we need neither the `let` block nor the macro at all. For two concrete examples

```julia
function multfunc1(r::Vector{Int})
    map(r) do x
        y -> y * x
    end
end

function multfunc2(r::Vector{Int})
    s = -r
    map(r) do x
        y -> y * x * s[1]
    end
end

```

So I came up with the idea of making a whitelist ((in a little genenal manner) of such use cases before the issue be solved 100% eventually. Hopefully there would be other cases that could be added into this whitelist for current v1.0. What do you think? Thanks!
