If a binding exists in the global scope, assign to that
s1_global = 0
println(s1_global)
for i=1:1
println(s1_global)
s1_global = 1
println(s1_global)
end
println(s1_global)
0 0 1 1
If no binding exists, make a new local binding
try
println(s1_local)
catch
println("not defined")
end
for i=1:1
try
println(s1_local)
catch
println("not defined")
end
s1_local = 1
try
println(s1_local)
catch
println("not defined")
end
end
try
println(s1_local)
catch
println("not defined")
end
not defined not defined 1 not defined
local assignment local x=1
makes a new local binding
s2 = 0
println(s2)
for i=1:1
try
println(s2)
catch
println("not defined")
end
local s2 = 1
println(s2)
end
println(s2)
0 not defined 1 0
Global assignment global x=1
makes a new global binding
try
println(s3)
catch
println("not defined")
end
for i=1:1
try
println(s3)
catch
println("not defined")
end
global s3 = 1
try
println(s3)
catch
println("not defined")
end
end
try
println(s3)
catch
println("not defined")
end
not defined not defined 1 1
normal assignment x = 1
make a new local binding
h1 = 0
println(h1)
function f()
try
println(h1)
catch
println("not defined")
end
h1 = 1
println(h1)
end
f()
println(h1)
0 not defined 1 0
local assignment local x=1
makes a new local binding (equivalent to above)
h2 = 0
println(h2)
function f()
try
println(h2)
catch
println("not defined")
end
local h2 = 1
println(h2)
end
f()
println(h2)
0 not defined 1 0
global assignment global x=1
makes a new global binding
try
println(h3)
catch
println("not defined")
end
function f()
try
println(h3)
catch
println("not defined")
end
global h3 = 1
println(h3)
end
f()
println(h3)
not defined not defined 1 1
Soft scope and hard scope varies only at the treatment of normal assignment. Soft scope always tries to find a global binding first instead of creating a new local variable as hard scope do.