2
2
3
3
The * scope* of a variable is the region of code within which a variable is visible. Variable scoping
4
4
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
6
6
where different blocks of code can use the same name without referring to the same thing. The
7
7
rules for when the same variable name does or doesn't refer to the same thing are called scope
8
8
rules; this section spells them out in detail.
9
9
10
10
Certain constructs in the language introduce * scope blocks* , which are regions of code that are
11
11
eligible to be the scope of some set of variables. The scope of a variable cannot be an arbitrary
12
12
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
14
14
constructs introducing scope blocks are:
15
15
16
16
# [ ] (@id man-scope-table)
@@ -19,19 +19,19 @@ constructs introducing scope blocks are:
19
19
20
20
- global scope
21
21
22
- + module, baremodule
22
+ + [ ` module ` ] ( @ref ) , [ ` baremodule ` ] ( @ref )
23
23
24
24
+ at interactive prompt (REPL)
25
25
26
26
- local scope (don't allow nesting)
27
27
28
- + (mutable) struct, macro
28
+ + (mutable) [ ` struct ` ] ( @ref ) , [ ` macro ` ] ( @ref )
29
29
30
30
* Scope blocks which may nest anywhere (in global or local scope):
31
31
32
32
- local scope
33
33
34
- + for, while, try-catch-finally, let
34
+ + [ ` for ` ] ( @ref ) , [ ` while ` ] ( @ref ) , [ ` try-catch-finally ` ] ( @ ref try), [ ` let ` ] ( @ref )
35
35
36
36
+ functions (either syntax, anonymous & do-blocks)
37
37
@@ -110,8 +110,8 @@ A new local scope is introduced by most code blocks (see above
110
110
[ table] (@ref man-scope-table) for a complete list).
111
111
A local scope inherits all the variables from a parent local scope,
112
112
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).
115
115
Unlike global scopes, local scopes are not namespaces,
116
116
thus variables in an inner scope cannot be retrieved from the parent scope through some sort of
117
117
qualified access.
@@ -130,10 +130,11 @@ julia> z
130
130
ERROR: UndefVarError: z not defined
131
131
```
132
132
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.
135
136
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:
137
138
138
139
``` jldoctest
139
140
julia> x = 0;
@@ -147,7 +148,7 @@ julia> x
147
148
0
148
149
```
149
150
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 ) :
151
152
152
153
``` jldoctest
153
154
julia> for i = 1:10
@@ -184,7 +185,7 @@ global scope block unless:
184
185
* an assignment would result in a modified * global* variable, or
185
186
* a variable is specifically marked with the keyword ` local ` .
186
187
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:
188
189
189
190
``` jldoctest
190
191
julia> x, y = 1, 2;
@@ -208,7 +209,7 @@ An explicit `global` is needed to assign to a global variable:
208
209
to be a programming best-practice.
209
210
One reason for this is that remotely changing the state of global variables in other
210
211
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 `
212
213
keyword to declare the intent to modify a global variable.
213
214
214
215
``` jldoctest
@@ -247,7 +248,7 @@ julia> x, y # verify that global x and y are unchanged
247
248
248
249
The reason to allow * modifying local* variables of parent scopes in
249
250
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
251
252
following example:
252
253
253
254
``` jldoctest
@@ -262,15 +263,15 @@ julia> counter()
262
263
2
263
264
```
264
265
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
267
268
from the enclosing scope by the inner function is sometimes called a
268
269
* captured* variable. Captured variables can present performance challenges
269
270
discussed in [ performance tips] (@ref man-performance-tips).
270
271
271
272
The distinction between inheriting global scope and nesting local scope
272
273
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.
274
275
Consider the modification of the last example by moving ` bar ` to the global scope:
275
276
276
277
``` jldoctest
@@ -467,7 +468,7 @@ julia> f()
467
468
## Constants
468
469
469
470
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:
471
472
472
473
``` jldoctest
473
474
julia> const e = 2.71828182845904523536;
0 commit comments