Understanding macro lexical bindings

While @Benny’s answer is of course true, I think a potentially more helpful answer is that you need to do the loop over i inside the macro, rather than leaving it as quoted code.

i.e. something like

macro dlamdba(body...)
    f_body = Expr(:block)
    for i ∈ 1:2
        ex = quote
            println($i)
            if msg == $(esc(body[i].args[1]))
                println(msg)
                $(esc(body[i].args[2]))()
            end
        end
        push!(f_body.args, ex)
    end
    :(msg -> $f_body)
end
4 Likes