Skip to content

Commit 0ecaffb

Browse files
authored
some documentation improvements (#30697)
fix #29244, document `Expr` fix #30648, link `isdefined` to `isassigned` update docstring for `macro` fix variable scope description in manual
1 parent 88e1cb0 commit 0ecaffb

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

base/docs/basedocs.jl

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ kw"primitive type"
129129
"""
130130
macro
131131
132-
`macro` defines a method to include generated code in the final body of a program. A
133-
macro maps a tuple of arguments to a returned expression, and the resulting expression
134-
is compiled directly rather than requiring a runtime `eval` call. Macro arguments may
135-
include expressions, literal values, and symbols. For example:
132+
`macro` defines a method for inserting generated code into a program.
133+
A macro maps a sequence of argument expressions to a returned expression, and the
134+
resulting expression is substituted directly into the program at the point where
135+
the macro is invoked.
136+
Macros are a way to run generated code without calling `eval`, since the generated
137+
code instead simply becomes part of the surrounding program.
138+
Macro arguments may include expressions, literal values, and symbols.
136139
137140
# Examples
138141
```jldoctest
@@ -237,6 +240,34 @@ For other purposes, `:( ... )` and `quote .. end` blocks are treated identically
237240
"""
238241
kw"quote"
239242

243+
"""
244+
Expr(head::Symbol, args...)
245+
246+
A type representing compound expressions in parsed julia code (ASTs).
247+
Each expression consists of a `head` Symbol identifying which kind of
248+
expression it is (e.g. a call, for loop, conditional statement, etc.),
249+
and subexpressions (e.g. the arguments of a call).
250+
The subexpressions are stored in a `Vector{Any}` field called `args`.
251+
252+
See the manual chapter on [Metaprogramming](@ref) and the developer
253+
documentation [Julia ASTs](@ref).
254+
255+
# Examples
256+
```jldoctest
257+
julia> Expr(:call, :+, 1, 2)
258+
:(1 + 2)
259+
260+
julia> dump(:(a ? b : c))
261+
Expr
262+
head: Symbol if
263+
args: Array{Any}((3,))
264+
1: Symbol a
265+
2: Symbol b
266+
3: Symbol c
267+
```
268+
"""
269+
Expr
270+
240271
"""
241272
'
242273
@@ -1351,9 +1382,11 @@ typeof
13511382
isdefined(object, s::Symbol)
13521383
isdefined(object, index::Int)
13531384
1354-
Tests whether an assignable location is defined. The arguments can be a module and a symbol
1385+
Tests whether a global variable or object field is defined. The arguments can be a module and a symbol
13551386
or a composite object and field name (as a symbol) or index.
13561387
1388+
To test whether an array element is defined, use [`isassigned`](@ref) instead.
1389+
13571390
# Examples
13581391
```jldoctest
13591392
julia> isdefined(Base, :sum)

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ comprehensions, broadcast-fusing | local | global or local
2626

2727
Notably missing from this table are
2828
[begin blocks](@ref man-compound-expressions) and [if blocks](@ref man-conditional-evaluation)
29-
which do *not* introduce new scope blocks.
29+
which do *not* introduce new scopes.
3030
Both types of scopes follow somewhat different rules which will be explained below.
3131

3232
Julia uses [lexical scoping](https://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scoping_vs._dynamic_scoping),
@@ -97,15 +97,12 @@ A new local scope is introduced by most code blocks (see above
9797
[table](@ref man-scope-table) for a complete list).
9898
A local scope inherits all the variables from a parent local scope,
9999
both for reading and writing.
100-
Additionally, the local scope inherits all global variables that are assigned
101-
in its parent global scope block (if it is surrounded by a global `if` or `begin` scope).
102100
Unlike global scopes, local scopes are not namespaces,
103101
thus variables in an inner scope cannot be retrieved from the parent scope through some sort of
104102
qualified access.
105103

106104
The following rules and examples pertain to local scopes.
107-
A newly introduced variable in a local scope does not
108-
back-propagate to its parent scope.
105+
A newly introduced variable in a local scope cannot be referenced by a parent scope.
109106
For example, here the ``z`` is not introduced into the top-level scope:
110107

111108
```jldoctest
@@ -121,18 +118,30 @@ ERROR: UndefVarError: z not defined
121118
In this and all following examples it is assumed that their top-level is a global scope
122119
with a clean workspace, for instance a newly started REPL.
123120

124-
Inside a local scope a variable can be forced to be a new local variable using the [`local`](@ref) keyword:
121+
Inner local scopes can, however, update variables in their parent scopes:
125122

126123
```jldoctest
127-
julia> x = 0;
124+
julia> for i = 1:1
125+
z = i
126+
for j = 1:1
127+
z = 0
128+
end
129+
println(z)
130+
end
131+
0
132+
```
128133

129-
julia> for i = 1:10
130-
local x # this is also the default
134+
Inside a local scope a variable can be forced to be a new local variable using the [`local`](@ref) keyword:
135+
136+
```jldoctest
137+
julia> for i = 1:1
131138
x = i + 1
139+
for j = 1:1
140+
local x = 0
141+
end
142+
println(x)
132143
end
133-
134-
julia> x
135-
0
144+
2
136145
```
137146

138147
Inside a local scope a global variable can be assigned to by using the keyword [`global`](@ref):
@@ -163,9 +172,6 @@ julia> z
163172
The `local` and `global` keywords can also be applied to destructuring assignments, e.g.
164173
`local x, y = 1, 2`. In this case the keyword affects all listed variables.
165174

166-
Local scopes are introduced by most block keywords,
167-
with notable exceptions of `begin` and `if`.
168-
169175
In a local scope, all variables are inherited from its parent
170176
global scope block unless:
171177

@@ -194,8 +200,8 @@ An explicit `global` is needed to assign to a global variable:
194200
!!! sidebar "Avoiding globals"
195201
Avoiding changing the value of global variables is considered by many
196202
to be a programming best-practice.
197-
One reason for this is that remotely changing the state of global variables in other
198-
modules should be done with care as it makes the local behavior of the program hard to reason about.
203+
Changing the value of a global variable can cause "action at a distance",
204+
making the behavior of a program harder to reason about.
199205
This is why the scope blocks that introduce local scope require the `global`
200206
keyword to declare the intent to modify a global variable.
201207

@@ -233,9 +239,9 @@ julia> x, y # verify that global x and y are unchanged
233239
(1, 2)
234240
```
235241

236-
The reason to allow *modifying local* variables of parent scopes in
242+
The reason to allow modifying local variables of parent scopes in
237243
nested functions is to allow constructing [`closures`](https://en.wikipedia.org/wiki/Closure_%28computer_programming%29)
238-
which have a private state, for instance the `state` variable in the
244+
which have private state, for instance the `state` variable in the
239245
following example:
240246

241247
```jldoctest

0 commit comments

Comments
 (0)