So it would strike me that with-defaults-bad (below) is a fairly
straightforward way of using destructuring in Clojure yet it can't be
done, instead of passing the same map in that's being destrucured you
need a new "type" that has keys being the variable names instead of
keys being keywords...  it would seem when using the :keys keyword
this shouldn't be necessary.

(defstruct params :x :y)

(def defaults (struct-map params :x 1 :y 2))

(defn no-defaults [{:keys [x y]}]
  (list x y))

;since this is thing that's being destructed, I'd like to provide one
as default
; but this won't work because the :or expects the var names not the
keys
(defn with-defaults-bad [{:keys [x y] :or defaults}]
  (list x y))

;note the absence of colons on the defaults matching the variables not
keys
;  but now I can't use my nice structure as a default
(defn with-defaults-good [{:keys [x y] :or {x 1 y 2}}]
  (list x y))


user> (no-defaults defaults)
(1 2)
user> (with-defaults-bad {:x 4 :y 5})
(4 5)
user> (with-defaults-bad {:x 4})
(4 nil)
user> (with-defaults-good {})
(1 2)
user> (with-defaults-good {:x 4})
(4 2)


minimally in the presence of the :keys feature in the paramaters it
would be nice if with-defaults-bad worked.  although there are other
ways to work around this - this way just seems "cleaner"


also presumably the value of all defaults is evaluated at call time,
right?


Kevin

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