In Julia the rules for accessing variables from other modules are explained in detail here.
The key issues in your situation are the following things:
- a variable is visible after
using
only if it is export
ed in the module
- you are allowed to access the variable in other modules
- you are not allowed to rebind a variable from other modules
This means that global variable binding creation operation is private to the module.
Here is a simple example module definition:
module M
export x
x = Int[]
function rebindx()
global x = Int[]
end
end
now assume you define and later use it in REPL (it could be any other module)
julia> module M
export x
x = Int[]
function rebindx()
global x = Int[]
end
end
Main.M
julia> using .M
Now you can access x
:
julia> x
0-element Array{Int64,1}
julia> push!(x, 1)
1-element Array{Int64,1}:
1
julia> x
1-element Array{Int64,1}:
1
julia> x[1] = 10
10
julia> x
1-element Array{Int64,1}:
10
But not rebind x
:
julia> x = 0
ERROR: cannot assign variable M.x from module Main
However, you can call a function defined inside M
module to change the binding of x
like this:
julia> x
1-element Array{Int64,1}:
10
julia> M.rebindx()
0-element Array{Int64,1}
julia> x
0-element Array{Int64,1}
This was possible because rebindx
was defined inside module M
so it has the right to change the binding of variable x
defined in this module.