On Tue, Apr 26, 2011 at 8:26 PM, Hugo Duncan <duncan.h...@gmail.com> wrote:
>
> 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]]]
>  …)
>

....
> 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]
>  …)
>
>
Here's an interesting alternative:

(defnkw foo
 "My foo"
 [positional1 ... positionalN
  & {:kw1 {:doc "The kw1 argument"
           :default default1}
     :kw2 {:doc "Another"
           :default blah
           :as wheee}
     :kw2 {}} :as options]

We can specify an :as key on the entire option map.

Maps are more flexible than vectors for attributes. :doc, :default, :as are
fairly descriptive
and other attributes can easily be added. There would be defaults for each
key.

I've put a spin on the CL defun shortcut for renaming keyword args (:kw2
will be bound to wheee).
See "Keyword Parameters": http://www.gigamonkeys.com/book/functions.html

This is an alternative which is much closer to CL's method:
& {[:kw2 wheee] {... atrs.. ]}

On the whole it seems pretty readable, but may be misleading with the { }
syntax.

It's also markedly more verbose.


> 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
>

-- 
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

Reply via email to