O'Reilly logo

Mastering Clojure Macros by Colin Jones

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required