Skip to content

Commit

Permalink
Merge pull request #2590 from Kodiologist/letw
Browse files Browse the repository at this point in the history
Warn that `let` is complex in the manual
  • Loading branch information
Kodiologist authored Jun 3, 2024
2 parents 4604638 + ce29871 commit 5db838b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 3 additions & 3 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Removals
* `(fn/a …)` is now `(fn :async …)`.
* `(with/a […] …)` is now `(with [:async …] …)`.

* As with `for`, `:async` must precede each name to be bound
asynchronously, because you can mix synchronous and asynchronous
types.
* As with `for`, `:async` must precede each name to be bound
asynchronously, because you can mix synchronous and asynchronous
types.

* `(yield-from …)` is now `(yield :from …)`.

Expand Down
17 changes: 13 additions & 4 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,19 @@ Assignment, mutation, and annotation

.. hy:macro:: (let [bindings #* body])
``let`` creates local variables with lexically scoped names. This form takes a
list of binding pairs followed by a ``body`` which gets executed. A let-bound
name ceases to refer to that local outside the ``let`` form, but arguments in
nested functions, and bindings in nested ``let`` forms, can shadow these names. ::
``let`` is a macro for simulating traditional block scoping as seen in other
Lisps. Since it coexists with ordinary Python scoping, its consequences can
be complex, so it's wise to get a solid understanding of Python scoping
before you use it. Beginners to Python should note particularly that
:hy:func:`setv` inside a function or class typically creates a local
variable, so ``let`` isn't required for local variables or closures as it is
in many other Lisps.

That disclaimer aside, ``let`` creates local variables with lexically scoped
names. The macro takes a list of binding pairs followed by a ``body`` which
gets executed. A let-bound name ceases to refer to that local outside the
``let`` form, but arguments in nested functions, and bindings in nested
``let`` forms, can shadow these names. ::

(let [x 5 y 6] ; Create `x` and `y`
(print x y) ; => 5 6
Expand Down

0 comments on commit 5db838b

Please sign in to comment.