<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged macro</title>
<link>https://ask.clojure.org/index.php/tag/macro</link>
<description></description>
<item>
<title>Why doesn't defn also implicitly name the anonymous function it defines for recursion?</title>
<link>https://ask.clojure.org/index.php/15000/doesnt-defn-implicitly-anonymous-function-defines-recursion</link>
<description>&lt;p&gt;Why doesn't &lt;code&gt;defn&lt;/code&gt; implicitly name the function to create a named recursion point?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(clojure.walk/macroexpand-all
  '(defn foo [x]
     (foo x)))
; =&amp;gt;
(def foo
  (fn*
    ([x]
     (foo x))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This could instead expand to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def foo
  (fn* foo ; note the foo here
    ([x]
     (foo x))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How it works right now, &lt;code&gt;foo&lt;/code&gt; resolves to a global var &lt;code&gt;#'foo&lt;/code&gt;, which is created when the &lt;code&gt;(def ...)&lt;/code&gt; form is analyzed. In the second case, it resolves directly to the function object. Thus, it should save us a var dereference at runtime in this case.&lt;/p&gt;
&lt;p&gt;EDIT: Found out, this is exactly what &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/reference/compilation#directlinking&quot;&gt;direct linking&lt;/a&gt; is used for.&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15000/doesnt-defn-implicitly-anonymous-function-defines-recursion</guid>
<pubDate>Sat, 21 Mar 2026 13:49:43 +0000</pubDate>
</item>
<item>
<title>specifying `:let` as the first element of `for` causes an macro expansion error</title>
<link>https://ask.clojure.org/index.php/14670/specifying-let-first-element-causes-macro-expansion-error</link>
<description>&lt;p&gt;We can use &lt;code&gt;:let&lt;/code&gt; in &lt;code&gt;for&lt;/code&gt; to define local variable like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(for [i (range 5)
      :let [inc-i (inc i)]]
  [i inc-i])
;;=&amp;gt; ([0 1] [1 2] [2 3] [3 4] [4 5])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise, if we specified &lt;code&gt;:let&lt;/code&gt; as first element, it causes an macro expansion error.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(for [:let [range-end 5]
      i (range range-end)
      :let [inc-i (inc i)]]
  [i inc-i])
;;=&amp;gt; Syntax error macroexpanding for at (*cider-scratch*:555:1).
;;   Can't pop empty vector
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, in this case, we should have wrapped &lt;code&gt;for&lt;/code&gt; in &lt;code&gt;let&lt;/code&gt;. However, I'm curious whether making &lt;code&gt;:let&lt;/code&gt; available only for second and subsequent elements is intended behavior as part of the public interface.&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14670/specifying-let-first-element-causes-macro-expansion-error</guid>
<pubDate>Sun, 10 Aug 2025 02:40:33 +0000</pubDate>
</item>
<item>
<title>define several Vars at once in the REPL (convenience fn/macro)</title>
<link>https://ask.clojure.org/index.php/13788/define-several-vars-at-once-in-the-repl-convenience-fn-macro</link>
<description>&lt;p&gt;context: I have parsed some extensive XML data from the internet. Now I want to explore/filter/drill down in the REPL.&lt;/p&gt;
&lt;p&gt;I have a LazySeq named &lt;code&gt;xs&lt;/code&gt; of unknown length and of unknown content. I would like to explore the first few elements of &lt;code&gt;xs&lt;/code&gt; at the REPL. For convenience, I would like to &lt;code&gt;def&lt;/code&gt; some Vars like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def x1 (nth xs 0 nil))
...
(def x5 (nth xs 4 nil))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is tedious to type, especially when it needs to be done repeatedly, as I develop my &quot;drill down&quot;-function. Is there a convenience function (or macro) that can do this for me, perhaps like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmany xs &quot;x&quot; 5)
;=&amp;gt; #'my.ns/x1
;=&amp;gt; ...
;=&amp;gt; #'my.ns/x5
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I tried to write a function for this. It uses &lt;code&gt;eval&lt;/code&gt; and is unsecure (and has other drawbacks as well). So, my questions are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Does something like this already exist somewhere?&lt;/li&gt;
&lt;li&gt;Do I need a macro for this or can it be done with a function? (I am unfamiliar with writing Clojure macros.)&lt;/li&gt;
&lt;li&gt;How can this function/macro be defined?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For amusements' sake, here is my attempt:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn def-many
  [xs prefix n]
  (map eval
       (for [i (range n)]
            (list 'def
                  (symbol (str prefix (inc i)))
                  (nth xs i nil)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(if &lt;code&gt;(nth xs i nil)&lt;/code&gt; is a LazySeq or a list then &lt;code&gt;eval&lt;/code&gt; will try to interpret it as a function call. This can be tested for and converted to a vector, for example. But yeah, this is not great.)&lt;/p&gt;
</description>
<category>REPL</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13788/define-several-vars-at-once-in-the-repl-convenience-fn-macro</guid>
<pubDate>Sat, 16 Mar 2024 21:44:23 +0000</pubDate>
</item>
<item>
<title>Macro does not work properly</title>
<link>https://ask.clojure.org/index.php/13127/macro-does-not-work-properly</link>
<description>&lt;pre&gt;&lt;code&gt;;;gets a list like (1 + (1 + 2))
;;returns (+ 1 (+ 1 2))
(defmacro infix [expression]
  (letfn [(helper [expression]
               (cond (not (list? expression)) expression
                     (= (count expression) 0) '()
                     (= (count expression) 1)  (recur (first expression))
                     (not (function? (first expression))) (let [a (first expression)
                                                                op (second expression)
                                                                b (nth expression 2)]
                                                                  (recur (cons (list op a b)
                                                                               (drop 3 expression))))
                    :else expression))]
  (helper expression)))

(macroexpand '(infix (1 + 1 * 2 * 4))) ;; =&amp;gt; ((+ 1 1) * 2 * 4)
(macroexpand '(infix (((1))))) ;; =&amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second recur seems to not work (in &lt;em&gt;(not (function? ....)&lt;/em&gt;). What is wrong with the code?&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13127/macro-does-not-work-properly</guid>
<pubDate>Thu, 03 Aug 2023 14:08:49 +0000</pubDate>
</item>
<item>
<title>Issue with ClojureScript macroexpansion equivalence</title>
<link>https://ask.clojure.org/index.php/12389/issue-with-clojurescript-macroexpansion-equivalence</link>
<description>&lt;p&gt;In ClojureScript  forms can't always be replaced by they macroexpanded forms. This is the case for defrecord and defn on its multi arity version.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version &quot;1.11.60&quot;}}}' -M -m cljs.main                                                                          
ClojureScript 1.11.60
                                                                                                                                                   
cljs.user=&amp;gt; (defrecord ARecord [a])                                                                                                                                     
cljs.user/ARecord                                                                                                                                                  

cljs.user=&amp;gt; (macroexpand '(defrecord ARecord [a]))
(let* [] (do (defrecord* ...)))

cljs.user=&amp;gt; (let* [] (do (defrecord* ...)))
WARNING: Wrong number of args (4) passed to ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to cljs.user/ARecord at line 1 &amp;lt;cljs repl&amp;gt;
WARNING: Wrong number of args (4) passed to cljs.user/ARecord at line 1 &amp;lt;cljs repl&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same thing happens trying to evaluate the forms resulting from this macroexpansion:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(macroexpand '(defn foo ([a] (foo a 5)) ([a b] (+ a b))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12389/issue-with-clojurescript-macroexpansion-equivalence</guid>
<pubDate>Tue, 15 Nov 2022 13:14:49 +0000</pubDate>
</item>
<item>
<title>Why (special-symbol? 'loop) returns false?</title>
<link>https://ask.clojure.org/index.php/12067/why-special-symbol-loop-returns-false</link>
<description>&lt;p&gt;Hi, I'm getting started with Clojure and I'm puzzled by the following:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(doc special-symbol?)&lt;/code&gt; states &quot;Returns &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;s&lt;/code&gt; names a &lt;strong&gt;special form&lt;/strong&gt;&quot;&lt;br&gt;
&lt;code&gt;(doc loop)&lt;/code&gt; states &quot;&lt;code&gt;clojure.core/loop&lt;/code&gt; … &lt;strong&gt;Special Form&lt;/strong&gt;: Evaluates the…&quot;&lt;br&gt;
But &lt;code&gt;(special-symbol? 'loop)&lt;/code&gt; returns false.&lt;/p&gt;
&lt;p&gt;Since &lt;code&gt;(source loop)&lt;/code&gt; reveals it is a &lt;code&gt;(defmacro …&lt;/code&gt;, I wonder if in the docs &lt;code&gt;loop&lt;/code&gt; should be called a &lt;em&gt;macro&lt;/em&gt; rather than a &lt;em&gt;special form&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;Also because &lt;code&gt;(special-form? 'def)&lt;/code&gt; returns true, and &lt;code&gt;def&lt;/code&gt; has no source, i.e. is not defined as a macro.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12067/why-special-symbol-loop-returns-false</guid>
<pubDate>Mon, 25 Jul 2022 12:01:36 +0000</pubDate>
</item>
<item>
<title>How do you access namespace values in a macro?</title>
<link>https://ask.clojure.org/index.php/10965/how-do-you-access-namespace-values-in-a-macro</link>
<description>&lt;p&gt;I'm working in clojurescript, where &lt;code&gt;ns-interns&lt;/code&gt; is a macro.&lt;/p&gt;
&lt;p&gt;I'm trying to write a macro that gets information about the internal bindings in a namespace, but I'm not able to manipulate the bindings at compile time.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro my-macro [namespace]
   (let [mappings (ns-interns namespace)]
           ; do stuff
       ))

(my-macro 'mylib.hi)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trouble is that the above fails because the &lt;code&gt;ns-interns&lt;/code&gt; macro think it's processing the symbol &lt;code&gt;namespace&lt;/code&gt;, and not the namespace I pass into &lt;code&gt;my-macro&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I can try&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[mappings `(ns-interns ~namespace)]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;, but then I can't expand the macro at compile-time into a map that I can use to generate code -- the only thing I can do is &quot;print&quot; it in the generated code. I tried using &lt;code&gt;macroexpand&lt;/code&gt; and &lt;code&gt;macroexpand-all&lt;/code&gt;, but they did not work for me -- I couldn't use them to get the results of &lt;code&gt;ns-interns&lt;/code&gt; for use in my macro.&lt;/p&gt;
&lt;p&gt;However, macros can be expanded, since the below will work if I hard-code the namespace symbol.  The trouble comes because I want to pass the outer macro's argument into an inner macro. What's the right way to solve approach this, where I want to get and manipulate the vars of a namespace at compile-time in Clojurescript?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro my-macro [namespace]
   (let [mappings (ns-interns 'mylib.hi)]
     ; do stuff
   )
)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10965/how-do-you-access-namespace-values-in-a-macro</guid>
<pubDate>Mon, 23 Aug 2021 15:58:29 +0000</pubDate>
</item>
<item>
<title>Macro defining function</title>
<link>https://ask.clojure.org/index.php/10911/macro-defining-function</link>
<description>&lt;p&gt;I am trying to do an exercise in Brave Clojure (chapter 8 exercise 3). I have simplified it to get to the bottom of my issue.&lt;/p&gt;
&lt;p&gt;Consider this setup:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def person {:first-name &quot;Louis&quot;
             :last-name &quot;Bourvil&quot;
             :age 32
             :city &quot;Nantes&quot;})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I'd like to be able to the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(create-func c-age :age)
(c-age person)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I know this works:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro create-func [func-name attr] `(def ~func-name ~attr))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But I was trying to do with a function and can't understand why this does not work:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro create-func [func-name attr] `(defn ~func-name [person] (~attr person)))
(create-func c-age :age)
; Syntax error macroexpanding clojure.core/defn at (REPL:1:1).
; user/person - failed: vector? at: [:fn-tail :arity-n :bodies :params] spec: 
:clojure.core.specs.alpha/param-list
; (user/person) - failed: Extra input at: [:fn-tail :arity-1 :params] spec: 
:clojure.core.specs.alpha/param-list
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(from the repl)&lt;/p&gt;
&lt;p&gt;Do you see where I'm wrong?&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10911/macro-defining-function</guid>
<pubDate>Fri, 06 Aug 2021 07:36:15 +0000</pubDate>
</item>
<item>
<title>Macro for threading lambda expression</title>
<link>https://ask.clojure.org/index.php/10749/macro-for-threading-lambda-expression</link>
<description>&lt;p&gt;It is common to have lambda functions with one argument for things like map, filter, find, etc.  &lt;/p&gt;
&lt;p&gt;I think could be usefull a macro that do something like &lt;code&gt;(fn-&amp;gt;  :value inc  (&amp;gt; 1))&lt;/code&gt; which would be equivalent of   &lt;code&gt;#(-&amp;gt; % :value inc  (&amp;gt; 1))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Something like  &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro fn-&amp;gt;
  [&amp;amp; forms]
  `(fn [x#] (-&amp;gt; x# ~@forms)))
(defmacro fn-&amp;gt;&amp;gt;
  [&amp;amp; forms]
  `(fn [x#] (-&amp;gt;&amp;gt; x# ~@forms)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I see this a very useful for readability if in the core library. wdyt?&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10749/macro-for-threading-lambda-expression</guid>
<pubDate>Fri, 02 Jul 2021 16:53:46 +0000</pubDate>
</item>
<item>
<title>How should I correctly detect a protocol symbol in a macro targeting CLJS?</title>
<link>https://ask.clojure.org/index.php/10731/should-correctly-detect-protocol-symbol-macro-targeting-cljs</link>
<description>&lt;p&gt;Up through Clojurescript 1.10.844, I could reliably tell whether a symbol &lt;code&gt;sym&lt;/code&gt; (at macro-expansion time) referred to a protocol by examining the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(:protocol-symbol (cljs.analyzer.api/resolve &amp;amp;env sym))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As of the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojurescript/commit/a8422ee060d7d98f7578c9acb2824a5e346e7958&quot;&gt;commit for CLJS-3276&lt;/a&gt;, this no longer works in my code. As far as I can tell, &lt;code&gt;defprotocol&lt;/code&gt; still attaches this data, and cljs.core &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojurescript/blob/ac23fec265bdf0ca971eb35c16da4b59191da5ca/src/main/clojure/cljs/core.cljc#L1421&quot;&gt;still uses this method internally&lt;/a&gt;. Is there a better / supported way to detect whether a symbol refers to a protocol? I'm very much hoping that this behavior is reintroduced, or else that there is some other reliable method.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10731/should-correctly-detect-protocol-symbol-macro-targeting-cljs</guid>
<pubDate>Fri, 25 Jun 2021 00:03:30 +0000</pubDate>
</item>
<item>
<title>Is the defn spec overly restrictive?</title>
<link>https://ask.clojure.org/index.php/10627/is-the-defn-spec-overly-restrictive</link>
<description>&lt;p&gt;It seems like it should be possible to use a macro for generating a attribute map for &lt;code&gt;defn&lt;/code&gt; but it appears as if the &lt;code&gt;defn&lt;/code&gt; spec requires a literal map. See example below, which makes the spec fail.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   (defmacro example [f output input]
     `{:test (fn [] (is (= (~f ~input) ~output)))})

   (defn tails
     (example tails
              [1 2 3 4]
              '([1 2 3 4] (2 3 4) (3 4) (4)))
     [coll]
     (take-while some? (iterate next coll)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;-&amp;gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Call to clojure.core/defn did not conform to spec.
       #:clojure.spec.alpha{:problems
                            ({:path [:fn-tail :arity-1 :params],
                              :pred clojure.core/vector?,
                              :val
                              (example
                               tails
                               [1 2 3 4]
                               '([1 2 3 4] (2 3 4) (3 4) (4))),
                              :via
                              [:clojure.core.specs.alpha/defn-args
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/param-list
                               :clojure.core.specs.alpha/param-list],
                              :in [1]}
                             {:path [:fn-tail :arity-n :bodies :params],
                              :pred clojure.core/vector?,
                              :val example,
                              :via
                              [:clojure.core.specs.alpha/defn-args
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/param-list
                               :clojure.core.specs.alpha/param-list],
                              :in [1 0]}),
                            :spec
                            #object[clojure.spec.alpha$regex_spec_impl$reify__2510 0x643aeb02 &quot;clojure.spec.alpha$regex_spec_impl$reify__2510@643aeb02&quot;],
                            :value
                            (tails
                             (example tails [1 2 3 4] '([1 2 3 4] (2 3 4) (3 4) (4)))
                             [coll]
                             (take-while some? (iterate next coll))),
                            :args
                            (tails
                             (example tails [1 2 3 4] '([1 2 3 4] (2 3 4) (3 4) (4)))
                             [coll]
                             (take-while some? (iterate next coll)))}
                     alpha.clj:  712  clojure.spec.alpha/macroexpand-check
                     alpha.clj:  704  clojure.spec.alpha/macroexpand-check
                      AFn.java:  156  clojure.lang.AFn/applyToHelper
                      AFn.java:  144  clojure.lang.AFn/applyTo
                      Var.java:  705  clojure.lang.Var/applyTo
                 Compiler.java: 6974  clojure.lang.Compiler/checkSpecs
                 Compiler.java: 6992  clojure.lang.Compiler/macroexpand1
                 Compiler.java: 7079  clojure.lang.Compiler/macroexpand
                 Compiler.java: 7165  clojure.lang.Compiler/eval
                 Compiler.java: 7136  clojure.lang.Compiler/eval
                      core.clj: 3202  clojure.core/eval
                      core.clj: 3198  clojure.core/eval
        interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                      AFn.java:  152  clojure.lang.AFn/applyToHelper
                      AFn.java:  144  clojure.lang.AFn/applyTo
                      core.clj:  667  clojure.core/apply
                      core.clj: 1977  clojure.core/with-bindings*
                      core.clj: 1977  clojure.core/with-bindings*
                   RestFn.java:  425  clojure.lang.RestFn/invoke
        interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                      main.clj:  437  clojure.main/repl/read-eval-print/fn
                      main.clj:  437  clojure.main/repl/read-eval-print
                      main.clj:  458  clojure.main/repl/fn
                      main.clj:  458  clojure.main/repl
                      main.clj:  368  clojure.main/repl
                   RestFn.java:  137  clojure.lang.RestFn/applyTo
                      core.clj:  667  clojure.core/apply
                      core.clj:  662  clojure.core/apply
                    regrow.clj:   20  refactor-nrepl.ns.slam.hound.regrow/wrap-clojure-repl/fn
                   RestFn.java: 1523  clojure.lang.RestFn/invoke
        interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
        interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
        interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                      AFn.java:   22  clojure.lang.AFn/run
                   session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
                   session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                      AFn.java:   22  clojure.lang.AFn/run
                   Thread.java:  829  java.lang.Thread/run
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10627/is-the-defn-spec-overly-restrictive</guid>
<pubDate>Tue, 18 May 2021 10:21:51 +0000</pubDate>
</item>
<item>
<title>macroexpansion of macro with anonymous function argument</title>
<link>https://ask.clojure.org/index.php/10269/macroexpansion-of-macro-with-anonymous-function-argument</link>
<description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I have following synthetic macro receiving function expression as argument:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro func-macro [f]
  `(def x ~f))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Macroexpansion gives me:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;( macroexpand-1 `(func-macro (fn [x] x)))
  -&amp;gt;
  (def runtime.repl/x (clojure.core/fn [runtime.repl/x] runtime.repl/x))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If i execute this code form in REPL then i ll get an error because function has namespace qualified symbol runtime.repl/x as argument.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(runtime.repl/x) - failed: Extra input at: [:fn-tail :arity-1 :params] spec: :clojure.core.specs.alpha/param-list
runtime.repl/x - failed: vector? at: [:fn-tail :arity-n :params] spec: :clojure.core.specs.alpha/param-list
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But if i am calling this macro in REPL then all things work!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;( (func-macro (fn [x] x)) 100)
    -&amp;gt;
    100
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why runtime.repl/x is OK for function definition if i am calling macro in REPL? &lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;p&gt;Andray&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10269/macroexpansion-of-macro-with-anonymous-function-argument</guid>
<pubDate>Sun, 28 Feb 2021 09:10:19 +0000</pubDate>
</item>
<item>
<title>case has incorrect + difficult-to-comprehend behavior matching 'quote on Clojure 1.10.0</title>
<link>https://ask.clojure.org/index.php/9508/incorrect-difficult-comprehend-behavior-matching-clojure</link>
<description>&lt;p&gt;I'm implementing a small Lisp interpreter, which led me to discover this bug in the implementation of &lt;code&gt;case&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case &quot;s&quot;
  'quote &quot;quote&quot;
  &quot;default&quot;)

;; Syntax error macroexpanding case at (org:localhost:56082(clj)*:131:16).
;; Duplicate case test constant: quote
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I thought this might be a good workaround:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case 'quote
  (symbol &quot;quote&quot;) &quot;quote&quot;
  &quot;default&quot;)
;; =&amp;gt; &quot;default&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This doesn't work, it returns &quot;default&quot;.&lt;/p&gt;
&lt;p&gt;Strangely, even though &lt;code&gt;(symbol quote)&lt;/code&gt; would be invalid, since &lt;code&gt;quote&lt;/code&gt; is not bound, THIS does &quot;work&quot; by returning &lt;code&gt;&quot;quote&quot;&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case 'quote
  (symbol quote) &quot;quote&quot;
  &quot;default&quot;)
;; =&amp;gt; &quot;quote&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Very weird. And look at this. Let's try a case of &lt;code&gt;'face&lt;/code&gt;. What will it return?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case 'quote
  'face &quot;face&quot;
  &quot;default&quot;)
;; =&amp;gt; &quot;face&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It matches &lt;code&gt;'quote&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;Actually anything quoted matches:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(case 'quote
  (quote 123) &quot;123&quot;
  &quot;default&quot;)
;; =&amp;gt; &quot;123&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; Hopefully this will be enough for this to make sense to someone with knowledge of &lt;code&gt;case&lt;/code&gt;'s macroexpansion.&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
</description>
<category>Errors</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9508/incorrect-difficult-comprehend-behavior-matching-clojure</guid>
<pubDate>Sun, 26 Jul 2020 21:18:09 +0000</pubDate>
</item>
<item>
<title>cljs.analyzer macroexpand doesn't work</title>
<link>https://ask.clojure.org/index.php/9489/cljs-analyzer-macroexpand-doesnt-work</link>
<description>&lt;p&gt;Related discussion:&lt;br&gt;
- &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure-emacs/cider/issues/2099#issuecomment-661940099&quot;&gt;https://github.com/clojure-emacs/cider/issues/2099#issuecomment-661940099&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns user
  (:require [cljs.analyzer :as analyzer]))

(let [ns-env (assoc-in (analyzer/empty-env) [:ns [:name]] 'cljs.user)]
  (analyzer/macroexpand-1 ns-env `(defn ~'foo [~'x] ~'x)))
;; (clojure.core/defn foo [x] x)

(macroexpand-1 `(defn ~'foo [~'x] ~'x))
;; (def foo (clojure.core/fn ([x] x)))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9489/cljs-analyzer-macroexpand-doesnt-work</guid>
<pubDate>Tue, 21 Jul 2020 16:13:21 +0000</pubDate>
</item>
</channel>
</rss>