Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Compiler by

The Clojure compiler appears to use the :arglists metadata of vars when compiling function invocations, in a way which conflicts with functions being called recursively in the body of their defn form with dynamically specified metadata.
A minimal example:

(defn foo
  {:arglists (list '[n])}
  (if (even? n)
    (foo (dec n))))

Which results in the compilation error:

Syntax error (ClassCastException) compiling foo at (REPL:6:5). class clojure.lang.Symbol cannot be cast to class clojure.lang.IPersistentVector (clojure.lang.Symbol and clojure.lang.IPersistentVector are in unnamed module of loader 'app')

The compiler appears to be using the unevaluated form of the arglist (list (quote [n])) when parsing the call to foo, throwing an exception due to the symbol list not being the expected argument vector.

Here's a slightly more realistic example of where this could occur:

(def ^:private shape-attrs
  '{:keys [height width color]})

(defn draw
  {:arglists (list ['shape]
                   ['shape shape-attrs])}
   (draw shape {:color "black"}))
  ([shape attrs]

1 Answer

0 votes

Var arglists should be a literal list of vectors, like:

:arglists '([shape] [shape shape-attrs])
I couldn't find this restriction mentioned anywhere, is it the case then that any programmatically defined arglists *must* be specified via `alter-meta!` after the var's creation?

Note that the following compiles and works just fine:
(defn draw
  {:arglists (list ['shape shape-attrs])}
  ([shape attrs]

the error only arises when a recursive call to the var is made within its own body.
I don't know that there is much or any documentation on :arglists, but I thinkyou can assume that the :arglists should be data in a defn.
A quick Github code search shows a few instances of this out in the wild, including a core Clojure lib:


Without explicit documentation or compiler warnings that only literal quoted lists were allowed in that particular metadata slot, I feel that it would be quite natural for people to assume otherwise.