gensym

clojure.core

  • (gensym)
  • (gensym prefix-string)
Returns a new symbol with a unique name. If a prefix string is
supplied, the name is prefix# where # is some unique number. If
prefix is not supplied, the prefix is 'G__'.

3 Examples top

  • user=> (gensym "foo")
    foo2020
    
    user=> (gensym "foo")
    foo2027
    
    user=> (gensym "foo")
    ;; ...
    
  • user=> (gensym)
    G__2034
    
    user=> (let [my-unique-sym (gensym)]
             my-unique-sym)
    G__2075
    
  • ;; syntax-reader uses gensym for non-namespace-qualified symbols ending with '#'
    ;; http://clojure.org/reader
    
    user=> `(name0#)       ; gensym, form is useful in defmacro
    (name0__1206__auto__)
    
    user=> `(user/name1#)  ; no gensym, namespace-qualified
    (user/name1#)
    
    user=> `(:key0#)       ; no gensym, keyword
    (:key0#)
    
    user=> `(::key1#)      ; no gensym, keyword
    (:user/key1#)
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:525 top

(defn gensym
  "Returns a new symbol with a unique name. If a prefix string is
  supplied, the name is prefix# where # is some unique number. If
  prefix is not supplied, the prefix is 'G__'."
  {:added "1.0"
   :static true}
  ([] (gensym "G__"))
  ([prefix-string] (. clojure.lang.Symbol (intern (str prefix-string (str (. clojure.lang.RT (nextID))))))))
Vars in clojure.core/gensym:
Used in 0 other vars

Comments top

1 comment(s) for gensym.

The

(. clojure.lang.RT (nextID))
present gensym's source code (https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/RT.java#L468) uses java.util.concurrent.atomic.AtomicInteger (and has for the past 6 years, if you trust the Git history).

Log in to add a comment.