Search code examples
clojure

Why doesn't work : "First argument to defn must be a symbol"


Why do I get the error:

IllegalArgumentException First argument to defn must be a symbol  clojure.core/defn (core.clj:277)

When I try to define a function like this:

(defn (symbol "f[]") 1)

Or like this:

(defn (symbol "f") [] 1)

Why aren't those the equivalent of straight forward example below ?

(defn f [] 1)

This is esoteric I know: but it just occurred to me that I might want to name a function dynamically at some point. (No real use case here - just trying to understand Clojure's mind...)


Solution

  • When you pass arguments to a macro, they are not evaluated beforehand. Since defn is a macro, what you're passing it in those two cases are not equivalent.