title: Clojure: def discards ^:macro when used outside the top-level
body: <p>If I evaluate</p>
  
  <pre><code>(def ^:macro my-defn1 #'defn)
  </code></pre>
  
  <p>a macro named 'my-defn1' is defined, which I can use just like 'defn'.</p>
  
  <p>However, if I evaluate instead</p>
  
  <pre><code>(if true
    (def ^:macro my-defn2 #'defn))
  </code></pre>
  
  <p>the var for 'my-defn2' doesn't have the :macro metadata set and I can't use it as a macro, even though the 'def' form is equal to the previous case.</p>
  
  <p>Here is the complete code (<a href="http://cljbin.com/paste/52322ba5e4b0fa645e7f9243" rel="nofollow">http://cljbin.com/paste/52322ba5e4b0fa645e7f9243</a>):</p>
  
  <pre><code>(def ^:macro my-defn1 #'defn)
  
  (if true
    (def ^:macro my-defn2 #'defn))
  
  (println (meta #'my-defn1))    ; =&gt; contains :macro
  
  (println (meta #'my-defn2))    ; =&gt; doesn't contain :macro!
  
  (my-defn1 hello1 []
            (println "hello 1"))
  
  (hello1)                       ; =&gt; prints "hello 1"
  
  (my-defn2 hello2 []            ; =&gt; CompilerException: Unable to resolve 
    (println "hello 2"))         ;    symbol: hello2 in this context
  </code></pre>
  
  <p>What makes the behaviour different?</p>