REPL = read eval print loop. Step through the read-eval process.
READ: Clojure sees the string "(`a)"
, parses it and ends up with a data structure. At read time, reader macros are expanded and not much else happens. In this case, the reader expands the backquote and ends up with this:
user> (read-string "(`a)")
((quote user/a))
EVAL: Clojure tries to evaluate this object. Evaluation rules vary depending on what kind of object you're looking at.
- Some objects evaluate as themselves (numbers, strings, keywords etc.).
- A Symbol is evaluated by resolving it in some namespace to obtain some value (usually).
- A List is evaluated by macro-expanding the list until there are no macros left, then recursively evaluating the first item in the list to obtain some resulting value, then using the value of the first item in the list to decide what to do. If the first value is a special form, special stuff happens. Otherwise the first value is treated as a function and called with the values of the rest of the list (obtained by recursively evaluating all of the list's items) as parameters.
- etc.
Refer to clojure.lang.Compiler/analyzeSeq
in the Clojure source to see the evaluation rules for lists, or clojure.lang.Compiler/analyzeSymbol
for symbols. There are lots of other evaluation rules there.
Example
Suppose you do this:
user> (user/a)
The REPL ends up doing this internally:
user> (eval '(user/a))
Clojure sees that you're evaluating a list, so it evaluates all items in the list. The first (and only) item:
user> (eval 'user/a)
#<user$a__1811 user$a__1811@82c23d>
a
is not a special form and this list doesn't need to be macroexpanded, so the symbol a
is looked up in the namespace user
and the resulting value here is an fn
. So this fn
is called.
Your code
But instead you have this:
user> (eval '((quote user/a)))
Clojure evaluates the first item in the list, which is itself a list.
user> (eval '(quote user/a))
user/a
It evaluated the first item in this sub-list, quote
, which is a special form, so special rules apply and it returns its argument (the Symbol a
) un-evaluated.
The symbol a
is the value in this case as the fn
was the value up above. So Clojure treats the Symbol itself as a function and calls it. In Clojure, anything that implements the Ifn
interface is callable like an fn
. It so happens that clojure.lang.Symbol
implements Ifn
. A Symbol called as a function expects one parameter, a collection, and it looks itself up in that collection. It's meant to be used like this:
user> ('a {'a :foo})
:foo
This is what it tries to do here. But you aren't passing any parameters, so you get the error "Wrong number of args passed to: Symbol" (it expects a collection).
For your code to work you'd need two levels of eval
. This works, hopefully you can see why:
user> (eval '((eval (quote user/a))))
Hello, world
user> ((eval (first l)))
Hello, world
Note that in real code, using eval
directly is usually a really bad idea. Macros are a better idea by far. I'm only using it here for demonstration.
Look in Compiler.java
in the Clojure source to see how this all plays out. It's not too hard to follow.