Skip to content

Commit ed524c6

Browse files
authored
Merge pull request #2596 from Kodiologist/doc-local-mac
Document a catch regarding local macros
2 parents 6fc0170 + 6ffeee9 commit ed524c6

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

docs/macros.rst

+19-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ When to use what
3838

3939
The variety of options can be intimidating. In addition to all of Hy's features listed above, Python is a dynamic programming language that allows you to do a lot of things at run-time that other languages would blanch at. For example, you can dynamically define a new class by calling :class:`type`. So, watch out for cases where your first thought is to use a macro, but you don't actually need one.
4040

41-
When deciding what to use, a good rule of thumb is to use the least powerful option that suffices for the syntax, semantics, and performance that you want. So first, see if Python's dynamic features are enough. If they aren't, try a macro-like construct or a regular macro. If even those aren't enough, try a reader macro. Using the least powerful applicable option will help you avoid the :ref:`macro pitfalls described below <macro-pitfalls>`, as well as other headaches such as wanting to use a macro where a Python API needs a function. (For the sake of providing simpler examples, much of the below discussion will ignore this advice and consider macros that could easily be written as functions.)
41+
When deciding what to use, a good rule of thumb is to use the least powerful option that suffices for the syntax, semantics, and performance that you want. So first, see if Python's dynamic features are enough. If they aren't, try a macro-like construct or a regular macro. If even those aren't enough, try a reader macro. Using the least powerful applicable option will help you avoid the :ref:`macro pitfalls described below <macro-pitfalls>`, as well as other headaches such as wanting to use a macro where a Python API needs a function. But for the sake of providing simpler examples, much of the below discussion will ignore this advice and consider example macros that could easily be written as functions.
4242

4343
The basics
4444
----------
@@ -239,6 +239,23 @@ There are three scoped varieties of regular macro. First are **core macros**, wh
239239
'(print "Goodbye, world."))))
240240
(new-mac) ; => "Goodbye, world."
241241

242-
**Local macros** are associated with function, class, or comprehension scopes, like Python local variables. They come about when you call ``defmacro`` or ``require`` in an appropriate scope. You can call :hy:func:`local-macros <hy.core.macros.local-macros>` to view local macros, but adding or deleting elements is ineffective.
242+
**Local macros** are associated with function, class, or comprehension scopes, like Python local variables. They come about when you call ``defmacro`` or ``require`` in an appropriate scope. You can call :hy:func:`local-macros <hy.core.macros.local-macros>` to view local macros, but adding or deleting elements is ineffective. Beware that local macro definitions apply to the results of expanding other macros in the given context, and hence may not be as local as you expect::
243+
244+
(defmacro number []
245+
1)
246+
247+
(defmacro uses-number []
248+
'(number))
249+
250+
(defn f []
251+
(defmacro number []
252+
2)
253+
(uses-number))
254+
255+
(print (uses-number)) ; => 1
256+
(print (f)) ; => 2
257+
(print (uses-number)) ; => 1
258+
259+
For this reason, shadowing a core macro is risky even with a local macro.
243260

244261
Finally, ``_hy_reader_macros`` is a per-module dictionary like ``_hy_macros`` for reader macros, but here, the keys aren't mangled. There are no local reader macros, and there's no official way to introspect on Hy's handful of core reader macros. So, of the three scoped varieties of regular macro, reader macros most resemble global macros.

0 commit comments

Comments
 (0)