Hi,

Am 11.09.2008 um 00:07 schrieb Allen Rohner:

Maybe the solution is to use defn-. Scheme made me used to the local
function approach for encapsulation. I don't have a problem with doing

(defn foo [x]
    (defn bar [y]
      "doc string")
    (defn baz [z]
      "doc string2")
    (do_stuff (bar[x]))

except that the interior defns are public. To me, the local functions
are a stronger indication that bar and baz are *only* used inside foo.
That indication isn't as apparent with

(defn bar [y]
  ...)

(defn baz [z]
...)

(defn foo [x]
(do_stuff (bar x)))
The question is: is it necessary to have this indication? For me the
grouping is given by the namespace (or the file). I found the following
pattern quite often lately:

(defn- first-step
  "Do something."
  ...)

(defn- second-step
  "Do another thing."
  ...)

(defn the-global-glue
  [x]
  (-> x first-step second-step ... nth-step))

For me this is more readable than a cramped:

(defn the-global-glue
  (let [first-step ...
                   ...
        second-step ...
                    ...]
   (-> ....)))

As I said: This is mostly personal style. Your mileage may vary.

As an aside, how is (defn- ) supposed to work? private symbols cannot
be referred to outside of the namespace? The macroexpand looks
identical:

user=> (macroexpand '(defn- foo [x] x))
(def foo (clojure/fn ([x] x)))
user=> (macroexpand '(defn foo [x] x))
(def foo (clojure/fn ([x] x)))

Yes. private definitions are not accessible from outside the namespace.

(defn- foo [x] x)

is equivalent to

(def #^{:private true} foo (fn [x] x))

The meta-stuff is handled in the reader, IIUIC. So it doesn't show up in
the reader output and hence in the macro expansion.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to