I’m reading the section on the manual regarding the scope of variables:
It says that macros introduce a hard local scope. There is some ambiguity here which confused me a little since there are two ways in which macros enter your code: the first is when you define them, and the second is when you call them.
I assume this table is referring to the definition of macros. However, I am then confused by the following behaviour, which is why I went to the manual:
julia> module Foo using Distributed @everywhere x = 1 f() = print(x) end Main.Foo julia> Foo.f() ERROR: UndefVarError: x not defined Stacktrace:  f() at ./REPL:4  top-level scope at REPL:1
On the other hand, if you don’t wrap the code in a
julia> using Distributed julia> @everywhere x = 1 julia> f() = print(x) f (generic function with 1 method) julia> f() 1
This made me think that calling macros introduced a soft local scope. (Though I may have misunderstood the manual here; I’m not convinced I fully understand the distinction yet.) However, this seems special to the
julia> module Bar @eval x = 1 f() = print(x) end Main.Bar julia> Bar.f() 1
The behaviour is exactly the same if you put these commands into a
.jl file and run them using
Can anyone help explain what’s happening?