Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the idiomatic (defn -main ...) mean in a clojure program?

Tags:

clojure

I'm familiar with packages from e.g. Java and Lisp, but what I'm seeing in other people's code is some apparent idioms like calling the entry point '-main' and using a backtick for the namespace name in (in-ns `foo), that kind of thing. I don't see these spelled out anywhere in docs or tutorials. Is there some resource which explains such conventions around structuring programs?

Edit:

I think I must have picked up the backtick thing from this answer: Splitting a Clojure namespace over multiple files, which I stumbled across while trying to make sense of (defn -main ...). Given the author, I took it as best practice. However, now that I poke around in the Clojure sources he cites, I see that only the regular quote is used. I guess most likely it's a typo.

(Narrowed the scope of the question title accordingly)

like image 447
fizzer Avatar asked Mar 14 '11 22:03

fizzer


1 Answers

The default for gen-class is to use - as the prefix for method names of that class. Which is why -main is the default entry point for java -cp clojure.jar yourclass

Backticks qualify their argument with the current namespace, so (in-ns `foo) is the same as (in-ns 'current-namespace/foo) and I don't think that's particularly idiomatic. The idiomatic way is to put each namespace in its own file with (ns ...) at the top, and use or require them as needed.

like image 91
Joost Diepenmaat Avatar answered Oct 02 '22 12:10

Joost Diepenmaat