Use `global a = ...` Please see: http://docs.julialang.org/en/latest/manual/variables-and-scoping/#hard-local-scope
global variables are only inherited for reading but not for writing On Fri, Sep 23, 2016 at 12:00 AM, K leo <cnbiz...@gmail.com> wrote: > Sorry, this is not related to Dict at all. If I replace the "a=get..." > statement with simply "a=2", the global variable is no longer accessible. > What is wrong? > > > On Friday, September 23, 2016 at 11:52:24 AM UTC+8, K leo wrote: >> >> Calling "get" anywhere in a function makes a global variable undefined. >> Can anyone please help explaining the following? >> >> 1) without calling "get", the global variable is fine: >> >> a=0 >>> >>> Dicta = Dict{Int,Int}() >>> >>> function testGlobal() >>> >>> println(a) >>> >>> merge!(Dicta, Dict(1=>1)) >>> >>> # a=get(Dicta, 1, 0) >>> >>> println(a) >>> >>> nothing >>> >>> end >>> >>> >>> julia> testGlobal() >> 0 >> 0 >> >> 2) calling "get" (same code as above except uncommenting the "get" >> statement) and the global variable becomes undefined: >> >> a=0 >>> >>> Dicta = Dict{Int,Int}() >>> >>> function testGlobal() >>> >>> println(a) >>> >>> merge!(Dicta, Dict(1=>1)) >>> >>> a=get(Dicta, 1, 0) >>> >>> println(a) >>> >>> nothing >>> >>> end >>> >>> >>> julia> testGlobal() >> ERROR: UndefVarError: a not defined >> in testGlobal() at /xxx/testType.jl:4 >> >> 3) not calling the first println, the code works, but the global a is not >> set: >> >> a=0 >>> >>> Dicta = Dict{Int,Int}() >>> >>> function testGlobal() >>> >>> # println(a) >>> >>> merge!(Dicta, Dict(1=>1)) >>> >>> a=get(Dicta, 1, 0) >>> >>> println(a) >>> >>> nothing >>> >>> end >>> >>> >>> >> julia> testGlobal() >> 1 >> >> >>