While doing do block, I pay attention to the variables captured by the closure. I tried some example in the manual and the previous discussions (like recent thread ) 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
function outerfunc(r::T)
... ... # no new assignment for r
... do x
innerfunc(x,r)
end
end
This seems like a great use case for writing a macro. It should be possible to write a macro that takes a block like
f(x, y)
g(x)
into
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.
Really appreciate all the replies. I noticed that FastClosures is mentioned in the manual. However, in the most simple use case we need neither the let block nor the macro at all. For two concrete examples
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!