On Thursday, December 25, 2014 8:29:45 AM UTC+10, Milan Bouchet-Valat wrote:
>
> Le mercredi 24 décembre 2014 à 10:21 -0800, [email protected]
> <javascript:> a
> écrit :
> > Hi all,
> >
> > Near the top of the variables and scoping chapter of the
> > documentation, there's a list which is kicked off by, "These
> > constructs which introduce new variables into the current scope are as
> > follows:"). How could this be worded to express things the other way
> > round --- that is, "When assigning to a variable, you *usually* create
> > a new local; the only times you don't are when: ..."?
> The wording has recently been changed in the development version of the
> manual. See:
> http://docs.julialang.org/en/latest/manual/variables-and-scoping/
>
> If you can come up with a better wording, please submit a pull request.
> You can do that easily by clicking on Edit on GitHub in the top-right
> corner of the web page. Thanks!
>
> > My understanding thus far is: when assigning to a variable, the only
> > times you *don't* create a new local is when you're assigning to an
> > already existing outer local or outer global. Is that the case?
> I'd say that's right, but I may well be missing a special-case.
>
> > Finally then, if that's the case, what is going on here:
> >
> > ~~~julia
> > #!/usr/bin/julia
> >
> > n = 1 # top-level variable, aka global
>
I think the thing missing from the manual is if this assignment does in
fact create a global, or a name in some sort of file scope?
Cheers
Lex
> >
> > while n <= 3
> > println(n)
> > # Wait. We're assigning here within the `while` scope... Though it
> would
> > # be weird, shouldn't that make a new local `n` since we don't have
> `global n`
> > # written anywhere in this scope?
> No, `global n` is only needed when `n` is in the global scope. Here `n`
> is not global, it's local to the enclosing scope. The manual says:
>
> An assignment x = y introduces a new local variable x only if x is
> neither declared global nor introduced as local by any enclosing scope
> before or after the current line of code.
>
> In the present case, `n` is introduced by the enclosing scope, so
> `global` is not needed.
>
> Again, if you can improve the manual, feel free to make suggestions. It
> looks like the meaning of "global" could use some more explicit
> definition.
>
>
> Regards
>
> > n = n + 1
> > end
> >
> > # prints 1 2 3
> >
> > println("done, and now n = ", n) # prints 4
> > ~~~
> >
> > Thanks,
> > -- John
> >
> >
>
>