问题
When I define a private function in Clojure, I usually use a -
prefix as a visual indicator that the function cannot be used outside of my namespace, e.g.
(defn- -name []
(let [formatter (formatter "yyyy-MM-dd-HH-mm-ss-SSSS")]
(format "fixjure-%s" (unparse formatter (now)))))
But the -
prefix seems to also be a convention for public methods when using gen-class
.
Is there any generally accepted convention for defn-
'd functions in the Clojure community, or should I simply use non-prefixed names?
It seems that lots of code in clojure.contrib (may it rest in peace) uses normal names for private functions, so maybe that is best, but I really like the visual indicator--maybe my C / Perl background is just too strong! ;)
回答1:
There's not a convention; the visual indicator is prevalent in languages with no built-in notion of private functions. Since Clojure's functions defined with defn-
are not visible outside their namespace, there is no need to prefix functions with an uglifier ;)
So do what you but, but you should probably want to just do as the rest of community does and just name them normally! It'll make your life easier.
回答2:
I am unaware of any naming conventions but you can attach ^:private
metadata tag for defining private functions. This is exactly equivalent to defn-
, but is a little clearer, IMHO.
(defn ^:private foo [])
来源:https://stackoverflow.com/questions/10846423/is-there-a-clojure-convention-for-naming-private-functions