Suggestion: introduce (defn name attr-map? [params*] doc-string body) form.

9 views
Skip to first unread message

Christian Vest Hansen

unread,
Dec 18, 2008, 7:41:39 PM12/18/08
to clojure group
All.

I think it would be nice if the doc-string was allowed (in addition to
current behavior) to immediately follow the params vector in the
various defsomethings.

To the best of my knowledge, such a change would be non-breaking because,
a) It should be implemented such that (defn foo [] "bar") is a
function that always returns the string "bar" - like it does today,
and
b) The "eggs" string following the params vector in (defn spam []
"eggs" "pokemon") is completely harmless in current code.

Even though this change would introduce yet-another-way-to-def-fn, I
think it is a good change because it allows be to keep my [params] on
the same line as my defn's and still have a thorough doc-string. And
in my humble opinion, I think it improves the readability of function
definitions when the defn, name and [params] are on the same line
regardless of how long the doc-string is.

--
Venlig hilsen / Kind regards,
Christian Vest Hansen.

Michael Wood

unread,
Dec 19, 2008, 6:46:38 AM12/19/08
to clo...@googlegroups.com

Where would it go when you have multiple parameter lists and bodies?

(defn blah ([a] (do-something-with a)) ([a b] (do-something-with a b)))

--
Michael Wood <esio...@gmail.com>

Christian Vest Hansen

unread,
Dec 19, 2008, 7:06:01 AM12/19/08
to clo...@googlegroups.com
On Fri, Dec 19, 2008 at 12:46 PM, Michael Wood <esio...@gmail.com> wrote:
>
> Where would it go when you have multiple parameter lists and bodies?
>
> (defn blah ([a] (do-something-with a)) ([a b] (do-something-with a b)))

That case should be unchanged, and work as it does today.

>
> --
> Michael Wood <esio...@gmail.com>

Christian Vest Hansen

unread,
Dec 29, 2008, 1:59:01 PM12/29/08
to clo...@googlegroups.com
Rich & rest.

I have gone through the process of implementing this in defn. The
patch is inlined for commenting (if need be) and attached for accurate
application.

I declare my changes public domain, but I suppose I could fill out a
CA if it helps. I can also make a similar change for defmacro, if you
want.

But if you don't want this at all, then I'll go weep in a corner and
leave it at that.



$ svn diff
Index: src/clj/clojure/core.clj
===================================================================
--- src/clj/clojure/core.clj (revision 1188)
+++ src/clj/clojure/core.clj (working copy)
@@ -183,6 +183,7 @@
name (fn ([params* ] exprs*)+)) with any doc-string or attrs added
to the var metadata"
:arglists '([name doc-string? attr-map? [params*] body]
+ [name attr-map? [params*] doc-string body]
[name doc-string? attr-map? ([params*] body)+ attr-map?])}
defn (fn defn [name & fdecl]
(let [m (if (string? (first fdecl))
@@ -197,6 +198,13 @@
fdecl (if (map? (first fdecl))
(rest fdecl)
fdecl)
+ m (if (clojure.lang.Numbers/lt 2 (clojure.lang.RT/length fdecl))
+ (if (vector? (first fdecl))
+ (if (string? (second fdecl))
+ (if (m :doc) m (assoc m :doc (second fdecl)))
+ m)
+ m)
+ m)
fdecl (if (vector? (first fdecl))
(list fdecl)
fdecl)
doc-string.patch

Chouser

unread,
Jan 5, 2009, 10:17:54 AM1/5/09
to clo...@googlegroups.com
On Mon, Dec 29, 2008 at 1:59 PM, Christian Vest Hansen
<karma...@gmail.com> wrote:
>
> Rich & rest.
>
> I have gone through the process of implementing this in defn. The
> patch is inlined for commenting (if need be) and attached for accurate
> application.

For the record, this proposal has been considered and rejected:
http://clojure-log.n01se.net/date/2008-12-30.html#10:50

Thanks for your efforts, Christian!
--Chouser

Reply all
Reply to author
Forward
0 new messages