On Tue, 26 Apr 2011 07:27:52 -0400, Meikel Brandmeyer <m...@kotka.de> wrote:
clojure supports keyword arguments for a quote some time now.
(defn foo
[positional1 ... positionalN & {:keys [kw1 ... kwn]}]
...)
This is used extensively in Pallet but has a couple of drawbacks.
- Repetition of keywords to specify defaults
- Repetition of keywords and defaults in doc strings
- The defaulted argument values are not available as a map
- No flag for presence of specified keyword (à la CL)
Inspired by a discussion [1] on supporting shFlags in stevedore, I drafted
a defnkw [2] that allows for the following syntax:
(defnkw foo
"My foo"
[positional1 … positionalN
& [[kw1 description1 default1]
[kw2 description2]]]
…)
and automatically generates a suffix to the doc string.
(doc foo) =>
My foo
- kw1 description1. Default default1.
- kw2 description2.
The formatting of the generated doc string can obviously be improved, and
it is not obvious to me what syntax to use for naming the keyword option
map (:as). This approach would allow defaulted values to be merged back
into the option map. Support for presence flags would require an extra
element in the specification vector (e.g. [description default flag?])
An alternative would be to use a more defnk like syntax:
(defnkw foo
"My foo"
[positional1 … positionalN
& :kw1 [description1 default1]
:kw2 description2]
…)
I would be interested in any comments.
[1] https://github.com/pallet/stevedore/pull/1
[2]
https://github.com/pallet/common/blob/feature%2Fdefn-for-kw-args/src/pallet/common/core.clj
--
Hugo Duncan
--
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