开发者

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

开发者 https://www.devze.com 2023-02-16 23:56 出处:网络
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

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 th开发者_如何学Cink 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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消