Skip to content

Commit 4851fab

Browse files
authored
Merge pull request #29376 from JuliaLang/ksh/improvar
Some improvements to variables and scoping manual
2 parents 391f2dd + 4020729 commit 4851fab

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

doc/src/manual/variables-and-scoping.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
The *scope* of a variable is the region of code within which a variable is visible. Variable scoping
44
helps avoid variable naming conflicts. The concept is intuitive: two functions can both have arguments
5-
called `x` without the two `x`'s referring to the same thing. Similarly there are many other cases
5+
called `x` without the two `x`'s referring to the same thing. Similarly, there are many other cases
66
where different blocks of code can use the same name without referring to the same thing. The
77
rules for when the same variable name does or doesn't refer to the same thing are called scope
88
rules; this section spells them out in detail.
99

1010
Certain constructs in the language introduce *scope blocks*, which are regions of code that are
1111
eligible to be the scope of some set of variables. The scope of a variable cannot be an arbitrary
1212
set of source lines; instead, it will always line up with one of these blocks. There are two
13-
main types of scopes in Julia, *global scope* and *local scope*, the latter can be nested. The
13+
main types of scopes in Julia, *global scope* and *local scope*. The latter can be nested. The
1414
constructs introducing scope blocks are:
1515

1616
# [](@id man-scope-table)
@@ -19,19 +19,19 @@ constructs introducing scope blocks are:
1919

2020
- global scope
2121

22-
+ module, baremodule
22+
+ [`module`](@ref), [`baremodule`](@ref)
2323

2424
+ at interactive prompt (REPL)
2525

2626
- local scope (don't allow nesting)
2727

28-
+ (mutable) struct, macro
28+
+ (mutable) [`struct`](@ref), [`macro`](@ref)
2929

3030
* Scope blocks which may nest anywhere (in global or local scope):
3131

3232
- local scope
3333

34-
+ for, while, try-catch-finally, let
34+
+ [`for`](@ref), [`while`](@ref), [`try-catch-finally`](@ref try), [`let`](@ref)
3535

3636
+ functions (either syntax, anonymous & do-blocks)
3737

@@ -110,8 +110,8 @@ A new local scope is introduced by most code blocks (see above
110110
[table](@ref man-scope-table) for a complete list).
111111
A local scope inherits all the variables from a parent local scope,
112112
both for reading and writing.
113-
Additionally, the local scope inherits all globals that are assigned
114-
to in its parent global scope block (if it is surrounded by a global `if` or `begin` scope).
113+
Additionally, the local scope inherits all global variables that are assigned
114+
in its parent global scope block (if it is surrounded by a global `if` or `begin` scope).
115115
Unlike global scopes, local scopes are not namespaces,
116116
thus variables in an inner scope cannot be retrieved from the parent scope through some sort of
117117
qualified access.
@@ -130,10 +130,11 @@ julia> z
130130
ERROR: UndefVarError: z not defined
131131
```
132132

133-
(Note, in this and all following examples it is assumed that their top-level is a global scope
134-
with a clean workspace, for instance a newly started REPL.)
133+
!!! note
134+
In this and all following examples it is assumed that their top-level is a global scope
135+
with a clean workspace, for instance a newly started REPL.
135136

136-
Inside a local scope a variable can be forced to be a new local variable using the `local` keyword:
137+
Inside a local scope a variable can be forced to be a new local variable using the [`local`](@ref) keyword:
137138

138139
```jldoctest
139140
julia> x = 0;
@@ -147,7 +148,7 @@ julia> x
147148
0
148149
```
149150

150-
Inside a local scope a global variable can be assigned to by using the keyword `global`:
151+
Inside a local scope a global variable can be assigned to by using the keyword [`global`](@ref):
151152

152153
```jldoctest
153154
julia> for i = 1:10
@@ -184,7 +185,7 @@ global scope block unless:
184185
* an assignment would result in a modified *global* variable, or
185186
* a variable is specifically marked with the keyword `local`.
186187

187-
Thus global variables are only inherited for reading but not for writing:
188+
Thus global variables are only inherited for reading, not for writing:
188189

189190
```jldoctest
190191
julia> x, y = 1, 2;
@@ -208,7 +209,7 @@ An explicit `global` is needed to assign to a global variable:
208209
to be a programming best-practice.
209210
One reason for this is that remotely changing the state of global variables in other
210211
modules should be done with care as it makes the local behavior of the program hard to reason about.
211-
This is why the scope blocks that introduce local scope require the ``global``
212+
This is why the scope blocks that introduce local scope require the `global`
212213
keyword to declare the intent to modify a global variable.
213214

214215
```jldoctest
@@ -247,7 +248,7 @@ julia> x, y # verify that global x and y are unchanged
247248

248249
The reason to allow *modifying local* variables of parent scopes in
249250
nested functions is to allow constructing [`closures`](https://en.wikipedia.org/wiki/Closure_%28computer_programming%29)
250-
which have a private state, for instance the ``state`` variable in the
251+
which have a private state, for instance the `state` variable in the
251252
following example:
252253

253254
```jldoctest
@@ -262,15 +263,15 @@ julia> counter()
262263
2
263264
```
264265

265-
See also the closures in the examples in the next two sections. A variable
266-
such as `x` in the first example and `state` in the second that is inherited
266+
See also the closures in the examples in the next two sections. A variable,
267+
such as `x` in the first example and `state` in the second, that is inherited
267268
from the enclosing scope by the inner function is sometimes called a
268269
*captured* variable. Captured variables can present performance challenges
269270
discussed in [performance tips](@ref man-performance-tips).
270271

271272
The distinction between inheriting global scope and nesting local scope
272273
can lead to some slight differences between functions
273-
defined in local vs. global scopes for variable assignments.
274+
defined in local versus global scopes for variable assignments.
274275
Consider the modification of the last example by moving `bar` to the global scope:
275276

276277
```jldoctest
@@ -467,7 +468,7 @@ julia> f()
467468
## Constants
468469

469470
A common use of variables is giving names to specific, unchanging values. Such variables are only
470-
assigned once. This intent can be conveyed to the compiler using the `const` keyword:
471+
assigned once. This intent can be conveyed to the compiler using the [`const`](@ref) keyword:
471472

472473
```jldoctest
473474
julia> const e = 2.71828182845904523536;

0 commit comments

Comments
 (0)