You're viewing version 1.2.0 of defonce. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

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:4653 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: defmacro name apply concat let list seq vector when-not

Comments top

No comments for defonce. Log in to add a comment.