How can I splat expressions into quote blocks?

ex1 = quote
    a = 1
    a += 1
end
ex2 = quote
    a *= 2
    a += 3
end
quote
    $ex1...
    $ex2...
end

gives me

quote
    #= REPL[65]:2 =#
    begin
        #= REPL[61]:2 =#
        a = 1
        #= REPL[61]:3 =#
        a += 1
    end...
    #= REPL[65]:3 =#
    begin
        #= REPL[62]:2 =#
        a *= 2
        #= REPL[62]:3 =#
        a += 3
    end...
end

but I want

quote
    a = 1
    a += 1
    a *= 2
    a += 3
end
1 Like

Of course something like

function _cat_blocks(blocks::Expr...)
    Expr(:block, mapreduce(x -> (@assert x.head ≡ :block; x.args), vcat, blocks)...)
end

works, but I am wondering if there is built-in syntax for this.

What about

julia> quote
           $(ex1.args...)
           $(ex2.args...)
       end
quote
    #= REPL[20]:2 =#
    #= REPL[10]:2 =#
    a = 1
    #= REPL[10]:3 =#
    a += 1
    #= REPL[20]:3 =#
    #= REPL[11]:2 =#
    a *= 2
    #= REPL[11]:3 =#
    a += 3
end

?

3 Likes

Thanks, I always forget the ()s.

1 Like