Hi Stephen,
Thanks for the reply!
Such behavior would be quite surprising; to see why, desugar :keys:
I may well be missing something, but I'm not sure I see your problem here.
(defn foo [& {x :x y :y :as args :or {x "x-default" y "y-default"}}]
args)
This is checking input args for an :x keyword. If found, it's binding the
associated value to 'x. Else it's binding the default value to 'x.
Either way, it's also binding all the args as originally given to 'args.
So with (foo :y "boo!") we have at this point:
* All args as originally given, bound to 'args. So '(:y "boo!")
* A map of symbols to default values. So {x "x-default" y "y-default"}
* Bindings as a result of the destructuring. So [x "x-default" y "boo!"]
What I'm suggesting might be useful, is destructuring sugar to bind one
additional map: {:x "x-default" :y "boo!"}.
The clojure.core/destructure code is pretty hairy, so I may be missing some
implementation-specific reason why this would be difficult to do. But I
don't see anything logically inconsistent or surprising about wanting to do
it. It'd be just another binding, and so not outside the scope of what
destructure is tasked with doing.
Indeed, I find real-world situations coming up quite often where this would
be a useful convenience.
When you want this,
> (let [{blah} (merge my-defaults kwargs)]
>
Unfortunately (again, unless I'm missing something) this fails on two
counts. One, it loses the transparency of the defaults to callers (the
original motivation I described). Two, it loses the convenience that :or
gives of actually having the symbols automatically bound as well.
I.e. to really match the functionality I'm talking about, you'd need to get
more verbose:
(defn foo [& {:keys [x y] :as args}]
(let [merged-args (merge {:x "x-default"} args)
{:keys [x y]} merged-args]
merged-args))
And, indeed, the repetition in this form makes it clear (I think) why this
would ideally be the domain of the destructuring itself.
Does that make sense?
--
*Peter Taoussanis*
[email protected]
twitter.com/ptaoussanis
+66 (0)82 597 2595 (Thailand)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en