Variable declarations within let blocks

Is there any semantic difference between the following two forms?

let a = 2, b = 3
    a + b
end

and

let
    a = 2
    b = 3
    a + b
end

I’m specifically curious about whether these are equivalent with respect to assignments that introduce new variables. I am working on a macro which currently expands to the latter.

julia> function f1()
           a = 1
           b = 2
           let a = 2, b = 3
           end
           return (a, b)
       end
f1 (generic function with 1 method)

julia> function f2()
           a = 1
           b = 2
           let
           a = 2; b = 3
           end
           return (a, b)
       end
f2 (generic function with 1 method)

julia> f1()
(1, 2)

julia> f2()
(2, 3)

let creates a new local scope and similar to other form of local scope (loops and with slight difference, functions) you can create new local variables or override nested local variables in them.

The special feature of let is that you can specify local variables that is not inherited from the parent local scope, when you specify them in the argument of let.

6 Likes

Thank you, Yichao. I just realized that I was not seeing this difference due to testing in global scope.