*I cite what StefanKarpinski said in issue 423. Is it the ultimate answer?*
*StefanKarpinski *commented on 22 Feb 2012 
<https://github.com/JuliaLang/julia/issues/423#issuecomment-4116924>

Here's an idea for how to fix this. The idea is to have "hard scope" and 
"soft scope". Functions introduce "hard scope" whereas various blocks 
introduce "soft scope". The difference would that in a new hard scope, 
assignment without a "global" or (hypothetical) "outer" declaration always 
creates a new variable local to that scope, regardless of whether a 
variable with that name exists in the outer scope. In a new soft scope, 
assignment updates a variable in the outer scope if such a variable exists 
or creates a new inner-scoped variable if no such variable exists. Some 
examples of the proposed behavior (not how things work now):

x = 0
while x < 10
  println(x)
  x += 1
end

*Works:* prints 0 through 9.

x = 0
function step()
  println(x)
  x += 1
end
while x < 10
  step()
end

*Fails:* complains that x inside of step() is undefined.

On Tuesday, March 10, 2015 at 10:40:53 PM UTC+1, Mauro wrote:
>
> I think this is the soft vs hard scope issue.  See: 
> https://github.com/JuliaLang/julia/issues/9955 
>
> That issue could use some fleshing out though... 
>
> On Tue, 2015-03-10 at 20:03, Wendell Zheng <zhengw...@gmail.com 
> <javascript:>> wrote: 
> > *Input 1:* 
> > y = 0 
> > function foo() 
> >     y = 10 
> > end 
> > foo() 
> > y 
> > 
> > *Output 1:* 
> > 0 
> > 
> > *Input 2:* 
> > y = 0 
> > for i = 1:1 
> >     y = 10 
> > end 
> > y 
> > 
> > *Output 2:* 
> > 10 
> > 
> > In the first example, y introduces a local variable. 
> > In the second example, y is still a global variable. 
> > 
> > This is not consistent to what the official document said. 
> > 
> > I tried these examples in JuliaBox. 
>
>

Reply via email to