defonce

clojure.core

  • (defonce name expr)
defs name to have the root value of the expr iff the named var has no root value,
else expr is unevaluated

1 Example top

  • user> (defonce foo 5)
    #'user/foo
    
    user> foo
    5
    
    ;; defonce does nothing the second time
    user> (defonce foo 10)
    nil
    
    user> foo
    5
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:5124 top

(defmacro defonce
  "defs name to have the root value of the expr iff the named var has no root value,
  else expr is unevaluated"
  {:added "1.0"}
  [name expr]
  `(let [v# (def ~name)]
     (when-not (.hasRoot v#)
       (def ~name ~expr))))
Vars in clojure.core/defonce:
Used in 0 other vars

Comments top

1 comment(s) for defonce.

Note that this isn't thread safe. I.e. EXPR for the same NAME can be evaluated more than once.

Log in to add a comment.