Skip to content

Commit c3d7338

Browse files
authored
Improve docs about let blocks (#39003)
This section was probably written before the distinction between hard and soft scopes was added.
1 parent 42bb1b2 commit c3d7338

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

base/docs/basedocs.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,10 @@ kw"."
414414
"""
415415
let
416416
417-
`let` statements allocate new variable bindings each time they run. Whereas an
418-
assignment modifies an existing value location, `let` creates new locations. This
419-
difference is only detectable in the case of variables that outlive their scope via
417+
`let` statements create a new hard scope block and introduce new variable bindings
418+
each time they run. Whereas assignments might reassign a new value to an existing value location,
419+
`let` always creates a new location.
420+
This difference is only detectable in the case of variables that outlive their scope via
420421
closures. The `let` syntax accepts a comma-separated series of assignments and variable
421422
names:
422423

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ that location:
119119
1. **Existing local:** If `x` is *already a local variable*, then the existing local `x` is
120120
assigned;
121121
2. **Hard scope:** If `x` is *not already a local variable* and assignment occurs inside of any
122-
hard scope construct (i.e. within a let block, function or macro body, comprehension, or
122+
hard scope construct (i.e. within a `let` block, function or macro body, comprehension, or
123123
generator), a new local named `x` is created in the scope of the assignment;
124124
3. **Soft scope:** If `x` is *not already a local variable* and all of the scope constructs
125125
containing the assignment are soft scopes (loops, `try`/`catch` blocks, or `struct` blocks), the
@@ -458,8 +458,9 @@ file, if it behaves differently than it did in the REPL, then you will get a war
458458

459459
### Let Blocks
460460

461-
Unlike assignments to local variables, `let` statements allocate new variable bindings each time
462-
they run. An assignment modifies an existing value location, and `let` creates new locations.
461+
`let` statements create a new *hard scope* block (see above) and introduce new variable
462+
bindings each time they run. Whereas assignments might reassign a new value to an existing value location,
463+
`let` always creates a new location.
463464
This difference is usually not important, and is only detectable in the case of variables that
464465
outlive their scope via closures. The `let` syntax accepts a comma-separated series of assignments
465466
and variable names:
@@ -517,7 +518,7 @@ julia> Fs[2]()
517518
```
518519

519520
Since the `begin` construct does not introduce a new scope, it can be useful to use a zero-argument
520-
`let` to just introduce a new scope block without creating any new bindings:
521+
`let` to just introduce a new scope block without creating any new bindings immediately:
521522

522523
```jldoctest
523524
julia> let
@@ -531,7 +532,16 @@ julia> let
531532
```
532533

533534
Since `let` introduces a new scope block, the inner local `x` is a different variable than the
534-
outer local `x`.
535+
outer local `x`. This particular example is equivalent to:
536+
537+
```jldoctest
538+
julia> let x = 1
539+
let x = 2
540+
end
541+
x
542+
end
543+
1
544+
```
535545

536546
### Loops and Comprehensions
537547

0 commit comments

Comments
 (0)