13

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)

Community
  • 1
  • 1
fizzer
  • 13,551
  • 9
  • 39
  • 61
  • I've looked at a couple of similarly titled questions here, but they aren't what I'm looking for – fizzer Mar 14 '11 at 22:22
  • 1
    the backtick in the in-ns is an interesting (and seemingly undocumented) trick. To clarify your question, do you mean the organization of definitions within a single source file or the organization of all the files in their package structure? – jk. Mar 14 '11 at 23:06
  • 2
    The latter definitely. I *think* I saw the backtick thing in a post on this site. I'll try to track it down tomorrow. Need to sleep now. zzzzzzzzzzz – fizzer Mar 14 '11 at 23:36

1 Answers1

9

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.

Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53
  • ok, I found gen-class, thank you +1. As for the point about namespaces, I think I perhaps saw another structure with one source file defining (ns ...) and a bunch of others using (in-ns ...). – fizzer Mar 14 '11 at 23:30
  • See link in my question for example of this style – fizzer Mar 15 '11 at 20:45
  • @fizzer: yeah, I'm pretty certain that's a typo. And while splitting namespaces across files can be useful, I still wouldn't call it idiomatic. – Joost Diepenmaat Mar 15 '11 at 20:52