# Let expression problem

**URL:** <https://discourse.julialang.org/t/let-expression-problem/43734>\
**Category:** General Usage\
**Created:** [July 27, 2020, 12:56am UTC](https://discourse.julialang.org/t/let-expression-problem/43734 "2020-07-27T00:56:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [July 27, 2020, 12:56am UTC](https://discourse.julialang.org/t/let-expression-problem/43734/1 "2020-07-27T00:56:56Z")

</div>

```julia
        x=1
        y=2
        z=3
        u=4
        a=5
        v=6
        let x=11, y=11, z::Int=11, u::Int=11, v=(a=11; 22)
            x=22
            println("x = ", x)
            println("y = ", y)
            println("z = ", z)
            println("u = ", u)
            println("a = ", a)
            println("v = ", v)
            println(x+y+z+u+v+a)
         end
         x=11
         println("x = ", x)
         println("y = ", y)
         println("z = ", z)
         println("u = ", u)
         println("a = ", a)
         println("v = ", v)
         println(x+y+z+u+a+v)

```

I would expect a to be 11, after the let but when I run it , it says it is 5. Here is the output. Is this expected?

```julia
julia> x=1
1

julia> y=2
2

julia> z=3
3

julia> u=4
4

julia> a=5
5

julia> v=6
6

julia> let x=11, y=11, z::Int=11, u::Int=11, v=(a=11; 22)
           x=22
           println("x = ", x)
           println("y = ", y)
           println("z = ", z)
           println("u = ", u)
           println("a = ", a)
           println("v = ", v)
           println(x+y+z+u+v+a)
        end
x = 22
y = 11
z = 11
u = 11
a = 11
v = 22
88

julia> x=11
11

julia> println("x = ", x)
x = 11

julia> println("y = ", y)
y = 2

julia> println("z = ", z)
z = 3

julia> println("u = ", u)
u = 4

julia> println("a = ", a)
a = 5

julia> println("v = ", v)
v = 6

julia> println(x+y+z+u+a+v)
31

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 27, 2020, 7:54am UTC](https://discourse.julialang.org/t/let-expression-problem/43734/2 "2020-07-27T07:54:57Z")

</div>

> [@Qiyamah](#):
>
> I would expect a to be 11, after the let but when I run it , it says it is 5. Here is the output. Is this expected?

Yes, the assignment happens within a `let` block so outside it is unaffected. See

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 27, 2020, 7:59am UTC](https://discourse.julialang.org/t/let-expression-problem/43734/3 "2020-07-27T07:59:14Z")

</div>

I thought about this too, and hesitated to answer after I tried the following:

```julia
julia> function test()
           a=5
           v=5
           println("a=",a," v=",v)
           let v=(a=11;22)
               println("a=",a," v=",v)
           end
           println("a=",a," v=",v)
       end
test (generic function with 1 method)

julia> test()
a=5 v=5
a=11 v=22
a=11 v=5

```

Is there a difference between `let` in the REPL and `let` inside a function? Apparently, but is there something written about this difference? Didn’t found something detailed.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 27, 2020, 3:03pm UTC](https://discourse.julialang.org/t/let-expression-problem/43734/4 "2020-07-27T15:03:30Z")

</div>

This is my mental model of how `let` works:

```julia
let v = (a = 11, 22)
end

```

can be written as by expanding the `(;)` as

```julia
let
    a = 11
    tmp1 = 22
    let v = tmp1
    end
end

```

Then within a function, `a = 11` updates the outer local scope, while within the global scope the `a` inside `let` is global.

If updating in the latter case is desired,

```julia
v, a = 5, 5
let v = (global a = 11; 22)
end
v, a # 5, 11

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 27, 2020, 3:16pm UTC](https://discourse.julialang.org/t/let-expression-problem/43734/5 "2020-07-27T15:16:39Z")

</div>

I think the subtlety here is that:

```julia
let x = ...

```

behaves differently than

```julia
let
  x = ...

```

For example:

```julia
julia> let
         x = 1
         let x = 2
           x = 3
         end
         @show x
       end
x = 1
1

julia> let
         x = 1
         let 
           x = 2
           x = 3
         end
         @show x
       end
x = 3
3

```

The same issue is discussed in [Variable declarations within let blocks](https://discourse.julialang.org/t/variable-declarations-within-let-blocks/29882)

I actually don’t think the manual is very clear on this topic: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Let-Blocks-1)

In particular, it says

> An assignment modifies an existing value location, and `let` creates new locations. This difference is usually not important, and is only detectable in the case of variables that outlive their scope via closures.

I think something like the answer from [Variable declarations within let blocks](https://discourse.julialang.org/t/variable-declarations-within-let-blocks/29882) would be a good addition to that section of the manual.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 27, 2020, 4:00pm UTC](https://discourse.julialang.org/t/let-expression-problem/43734/6 "2020-07-27T16:00:13Z")

</div>

So

```julia
let v=(a=11;22)
    ...
end

```

is equivalent to

```julia
let v=22
    a=11
    ...
end

```

?  
If yes, all is clear.  
Actually I can check myself:

```julia
julia> function test()
       a=5
       v=5
       let v=(a=11;22)
       end
       (a,v)
       end
test (generic function with 1 method)

julia> function test2()
       a=5
       v=5
       let v=22
       a=11
       end
       (a,v)
       end
test2 (generic function with 1 method)

julia> @code_lowered test()
CodeInfo(
1 ─ a = 5
│ v@_3 = 5
│ a = 11
│ v@_4 = 22
│ %5 = Core.tuple(a, v@_3)
└── return %5
)

julia> @code_lowered test2()
CodeInfo(
1 ─ a = 5
│ v@_3 = 5
│ v@_4 = 22
│ a = 11
│ %5 = Core.tuple(a, v@_3)
└── return %5
)

```

Answer is yes 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 28, 2020, 9:08am UTC](https://discourse.julialang.org/t/let-expression-problem/43734/7 "2020-07-28T09:08:02Z")

</div>

Please consider making a PR to the docs.
