On Nov 14, 1:50 pm, John Harrop <jharrop...@gmail.com> wrote: > On Sat, Nov 14, 2009 at 8:55 AM, Albert Cardona <sapri...@gmail.com> wrote: > > On Fri, Nov 13, 2009 at 11:26 PM, Mike Hogye <stacktra...@gmail.com> > > wrote: > > > Why is there an easy way to def a private function (defn-), but no > > > similarly easy way to def an arbitrary var as private? > > > The way I see it, def- would encourage gratuitous variable declaration > > imperative style. > > > By private var what one means is a local variable. > > > If you want a private variable use a let binding: > > > (let [my-private-var "Something not to be shared"] > > (defn my-public-fn > > "Do something" > > [a b] > > (str a b my-private-var))) > > > If you need the private variable to be mutable, use a ref and dosync > > changes on it. That goes a long way towards correctness. > > I find top-level lets to be messy compared to defining global constants like > this: > > (def +the-constant+ value) > > There's a defvar that adds docstring capability to def, and a private-making > version defvar-, in clojure.contrib.def. > > My own style preference would be to put important constants in defs at the > top of the file, rather than use let or (worse) embed "magic numbers" or the > like in individual functions that might need to be tweaked. Constants that > are truly constant because they're forced to be by math, the algorithm used, > or whatever might be locally letted in the function that uses them, to name > them while keeping them with the rest of the algorithm, though.
Whether or not top level lets are messy, don't you take a performance hit for using a def? Semantically, I would think that the let is also closer to a constant, as it can't be rebound (whereas a def can be rebound thread-locally). Although I suppose the most constant-like solution would be to use a symbol macro (I think there is a version in contrib). -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en