Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>let</code> creates a lexically scoped immutable alias for some value. <code>binding</code> creates a dynamically scoped binding for some <code>Var</code>. </p> <p>Dynamic binding means that the code inside your <code>binding</code> form and any code which that code calls (even if not in the local lexical scope) will see the new binding.</p> <p>Given:</p> <pre><code>user&gt; (def ^:dynamic x 0) #'user/x </code></pre> <p><code>binding</code> actually creates a dynamic binding for a <code>Var</code> but <code>let</code> only shadows the var with a local alias:</p> <pre><code>user&gt; (binding [x 1] (var-get #'x)) 1 user&gt; (let [x 1] (var-get #'x)) 0 </code></pre> <p><code>binding</code> can use qualified names (since it operates on <code>Var</code>s) and <code>let</code> can't:</p> <pre><code>user&gt; (binding [user/x 1] (var-get #'x)) 1 user&gt; (let [user/x 1] (var-get #'x)) ; Evaluation aborted. ;; Can't let qualified name: user/x </code></pre> <p><code>let</code>-introduced bindings are not mutable. <code>binding</code>-introduced bindings are thread-locally mutable:</p> <pre><code>user&gt; (binding [x 1] (set! x 2) x) 2 user&gt; (let [x 1] (set! x 2) x) ; Evaluation aborted. ;; Invalid assignment target </code></pre> <p>Lexical vs. dynamic binding:</p> <pre><code>user&gt; (defn foo [] (println x)) #'user/foo user&gt; (binding [x 1] (foo)) 1 nil user&gt; (let [x 1] (foo)) 0 nil </code></pre> <p>See also <a href="http://clojure.org/vars" rel="noreferrer">Vars</a>, <a href="http://clojure.org/special_forms#let" rel="noreferrer">let</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload