Approaching Hygiene with the Gensym
In order to avoid symbol capture issues like the one we just saw, Clojure gives us a few tools, all related to the gensym function. gensym’s job is simple: it produces a symbol with a unique name. The names will look funny because the name needs to be unique for the application, but that’s OK because we never need to type them into code:
advanced_mechanics/gensym_2.clj | |
| user=> (gensym) |
| ;=> G__671 |
| user=> (gensym) |
| ;=> G__674 |
| user=> (gensym "xyz") |
| ;=> xyz677 |
| user=> (gensym "xyz") |
| ;=> xyz680 |
As you can see, any given invocation of gensym gives a unique value back—so if you want to refer to the same one twice, you’ll need to hold onto the value with a let binding or something similar. ...
Get Mastering Clojure Macros now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.