Scoping rules in try blocks

question

#1

Could anyone explain the scoping rules below (and if it is expected)? They seem rather unintuitive to me

julia> let
       try
           x = 2
       end
       y = x + 1
       end
ERROR: UndefVarError: x not defined

julia> let
       try
           x = 2
       end
       x = x + 1
       end
3

#2

Not just try block.
Assignment without global makes the variable local in the scope so in the first case x is local to the try and in the second case the x is local to the let.
This is similar to https://github.com/JuliaLang/julia/issues/14948


#3

But isn’t this still unintuitive that you can change the scope of x by calling x=x? Seems like a hack with potentially little harm however, because one would not call x = f(x) when x is not defined in the current scope unless referring to the previous x as the input to f so it kind of works out anyways, but still weird.


#4

The scope is defined by the outer most block that has an assignment


#5

Interesting.