# Constructing a let block

**URL:** <https://discourse.julialang.org/t/constructing-a-let-block/110270>\
**Category:** General Usage\
**Created:** [February 15, 2024, 8:39pm UTC](https://discourse.julialang.org/t/constructing-a-let-block/110270 "2024-02-15T20:39:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [February 15, 2024, 8:39pm UTC](https://discourse.julialang.org/t/constructing-a-let-block/110270/1 "2024-02-15T20:39:42Z")

</div>

Can someone help explain why the let block only works up to the second argument?

How would I rewrite this to account for all arguments?

```julia
julia> a = 4
4

julia> eval(Expr(:let, :(b = a), :(h = b+2), :(d = b+3)))
6

```

Thanks.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 15, 2024, 8:43pm UTC](https://discourse.julialang.org/t/constructing-a-let-block/110270/2 "2024-02-15T20:43:43Z")

</div>

Just ask Julia how it constructs the let block you actually wanted:

```Julia
julia> Meta.show_sexpr(:(let b=a
           h = b+2
           d = b+3
       end))
(:let, (:(=), :b, :a), (:block,
    :(#= REPL[16]:2 =#),
    (:(=), :h, (:call, :+, :b, 2)),
    :(#= REPL[16]:3 =#),
    (:(=), :d, (:call, :+, :b, 3))
  ))

```

Or more simply, that’s using a begin/end block to group the multiple expressions:

```julia
julia> eval(Expr(:let, :(b = a), :(begin; h = b+2; d = b+3; end)))
7

```

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [February 15, 2024, 8:55pm UTC](https://discourse.julialang.org/t/constructing-a-let-block/110270/3 "2024-02-15T20:55:33Z")

</div>

Is there a way to construct this? I tried the following:

```julia
julia> expr = Expr(:let)
:($(Expr(:let)))

julia> push!(expr.args, Expr(:(=), b, a))
1-element Vector{Any}:
 :(4 = 4)

julia> push!(expr.args, Expr(:block))
2-element Vector{Any}:
 :(4 = 4)
 quote
end

julia> push!(expr.args[2].args, Expr(:(=), h, b+2))
1-element Vector{Any}:
 :(4 = 6)

julia> eval(expr)
ERROR: syntax: invalid let syntax
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval
   @ Core .\boot.jl:385 [inlined]
 [3] eval(x::Expr)
   @ Base.MainInclude .\client.jl:491
 [4] top-level scope
   @ REPL[150]:1

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 15, 2024, 8:58pm UTC](https://discourse.julialang.org/t/constructing-a-let-block/110270/4 "2024-02-15T20:58:03Z")

</div>

You’re missing a good deal of quoting there. You want:

```julia
expr = Expr(:let)
push!(expr.args, Expr(:(=), :b, :a))
push!(expr.args, Expr(:block))
push!(expr.args[2].args, Expr(:(=), :h, :(b+2)))

```
