Is there a reasonable way to avoid the begin
block wrapping the nested let
block here:
function buildexpr(x::Tuple)
subexprs = map(x) do xi
buildexpr(xi)
end
quote
let
$(subexprs...)
foo($x)
end
end
end
buildexpr(x) = :($x)
julia> buildexpr((1,2,(3,4)))
quote
#= REPL[289]:6 =#
let
#= REPL[289]:7 =#
1
2
begin
#= REPL[289]:6 =#
let
#= REPL[289]:7 =#
3
4
#= REPL[289]:8 =#
foo((3, 4))
end
end
#= REPL[289]:8 =#
foo((1, 2, (3, 4)))
end
end
I guess they don't do anything bad, but I would like to make the generated expression a bit more readable as there might be many levels.
julia> function buildexpr(x::Tuple)
subexprs = map(x) do xi
buildexpr(xi)
end
:(let
$(subexprs...)
foo($x)
end)
end
buildexpr (generic function with 2 methods)
julia> buildexpr((1,2,(3,4)))
:(let
#= REPL[185]:6 =#
1
2
let
#= REPL[185]:6 =#
3
4
#= REPL[185]:7 =#
foo((3, 4))
end
#= REPL[185]:7 =#
foo((1, 2, (3, 4)))
end)
Wow, here I have been thinking for the longest time that qoute
blocks are just a way to do :()
with fewer parentheses. The manual seems to just kinda introduce them like they are equivalent.
:()
is more like a quasiquote with interpolation
without interpolation, it's a "return the literal expression written here" thing
Yeah, that's how I mapped it to my Lisp brain as well.
I use that pattern all the time for combining multiple let blocks into one, though not in Julia...yet :smile:
@Sukera Why do you have to distinguish between those two cases?
because the interpolate part gets interpolated into the returned expression, instead of being an interpolate
block in the expression
@Sukera This was how I thought about both qoute
and :()
. My revised intuition is that they are equivalent, its just that a quote
adds a begin
block when lowered (or something) which happened to show up here.
Basically, quoting is not the same as quasiquoting
as in, the returned expression has no idea it got interpolated into
Last updated: Nov 06 2024 at 04:40 UTC