Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Clojure compiled or interpreted?

I read somewhere Clojure is compiled. Is it really compiled, like Java or Scala, rather than interpreted, like Jython or JRuby?

like image 368
OscarRyz Avatar asked Apr 14 '11 21:04

OscarRyz


People also ask

Does clojure compile to Java bytecode?

Clojure is always compiled. The Clojure compiler produces Java byte code, which is typically then JIT-compiled to native code by the JVM.

Does clojure run on JVM?

Clojure is the result of all this. It's a LISP functional programming language running on the JVM designed for concurrent programs.

What is AOT in Clojure?

Clojure compiles all code you load on-the-fly into JVM bytecode, but sometimes it is advantageous to compile ahead-of-time (AOT). Some reasons to use AOT compilation are: To deliver your application without source. To speed up application startup. To generate named classes for use by Java.


1 Answers

Clojure is always compiled.

The Clojure compiler produces Java byte code, which is typically then JIT-compiled to native code by the JVM.

The thing that can be confusing is the dynamic and interactive nature of Clojure that means you can invoke the compiler at run-time if you want to. This is all part of the Lisp "code is data" tradition.

For example, the following will invoke the Clojure compiler at run-time to compile and execute the form (+ 1 2):

(eval '(+ 1 2)) => 3 

The ability to invoke the compiler at run-time is very useful - for example it enables you to compile and run new code in the middle of a running Clojure application by using the REPL. But it's important not to confuse this "interactive" style of development with being "interpreted" - Clojure development is interactive, but still always compiled.

like image 115
mikera Avatar answered Sep 19 '22 05:09

mikera