Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do clojure programs/compilers/interpreters actually "work"?

It seems that to install Clojure in every new IDE, I have to completely re-install it and create a copy of it. And running the REPL seems like it's running a Java program.

I'm coming from a Ruby background, where Ruby programs are run by ruby program.rb, and the ruby being a program executed from one place, onto the file (I am aware that this is similar to how it works for java, python, etc. as well).

Is the clojure "interpreter" merely a compiled Java file which takes the .clj file as an argument, and does stuff to it?

like image 319
Justin L. Avatar asked Jun 24 '10 04:06

Justin L.


Video Answer


1 Answers

Firstly, Clojure has no interpreter. All Clojure code is compiled into JVM bytecode when it is being loaded. I'm stressing this point, because this is were Clojure's excellent performance story begins.

Secondly, you don't really "install" Clojure in the sense that you do Ruby. Clojure comes as a jar file, which is just a bunch of Java classes; if you put the jar file on your classpath, you can run methods of those classes. Of those classes, clojure.main provides a main method for running REPLs and "scripts". So, running the REPL is indeed running a Java (that is, JVM) programme; and running a clj file amounts to asking clojure.main to load and run it (the actual work is handed off to other classes in Clojure's implementation, but clojure.main is the entry point). BTW, this is exactly the same as with JRuby.

Every JVM programme is ultimately "merely a compiled Java file", or perhaps a bunch of such files. To run it, you need to have a JVM instance load it and run the appropriate main method. Note that C programmes (such as ruby-the-command) are only different in that the operating system knows how to find their main functions for you (well, the equivalent of Java's classpath works pretty differently too, but the main concepts are the same). With JVM programmes, you need to use an OS-friendly executable (java / java.exe) to kick things off.

like image 183
Michał Marczyk Avatar answered Nov 10 '22 06:11

Michał Marczyk