zcaudate
zcaudate

Reputation: 14258

What are the steps involved in compiling a clojure file?

I'm curious to know what actually happens when a clojure file is compiled into class files. What happens with macros when a file is aot compiled?

Is there any difference between repl evaluation and compilation?

Upvotes: 4

Views: 131

Answers (1)

WeGi
WeGi

Reputation: 1926

Regarding the Macros Thumbnails Comment is right. The Reader allways first evaluates the macros before anything else, like this.

Evaluation Cycle of Clojure

This is the reason some things can be done as Macro and not as a function. Example: or is a Macro for a simple reason. Lets look how we would define or as a macro or as a function.

(defmacro or
  ([] nil)
  ([x] x)
  ([x & next]
      `(let [or# ~x]
         (if or# or# (or ~@next)))))


(defn or
  ([] nil)
  ([arg] arg)
  ([arg & args]
    (if arg arg (or args))

Now if you would try to call both functions with say (or 1 (println :foo)) The macro would simply return 1, whereby the function would return 1 and print ":foo".

This is explained by the picture above. The reader first expands the source code, by restructuring it and then evaluates the arguments. The Function first evaluates the arguments and then the body.

The Compiling to a class would be somewhere around the evaluation Step.

Upvotes: 2

Related Questions