'How to think of a 'begin' block in Julia
I'm using Julia v1.6.3 in Atom. According to Julia's documentation, a begin block does not introduce a new scope. Now, consider these two blocks of code:
x = 1
begin
local x = 0
for i = 1:10
x += 10
end
end
println(x)
These lines would print out 1. I'm guessing what happened is a local x is created within the begin block, and the for loop would use and modify that local x. As a result, the x in the main scope is not changed.
The second block of code is as follows:
x = 1
local x = 0
for i = 1:10
x += 10
end
println(x)
These lines would print out 101. I'm guessing the "local x = 0" statement didn't change anything in the main scope, and in the for loop we are using the global variable x assigned at the beginning.
So surely the begin block does something here, but I'm not exactly sure how to think of it if we don't think of it as some sort of "new scope".
Solution 1:[1]
Yes, "begin block does not introduce new scope," by itself, without the local keyword. However a local keyword declaration of a variable is nevertheless local to the scope of an enclosing begin/end block.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill |
