Skip to content

Commit

Permalink
added parameter destructuring support to reify and deftype/record
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Apr 27, 2010
1 parent db3466e commit bf8bb79
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3043,6 +3043,24 @@
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings) ~@body))

(defn ^{:private true}
maybe-destructured
[params body]
(if (every? symbol? params)
(cons params body)
(loop [params params
new-params []
lets []]
(if params
(if (symbol? (first params))
(recur (next params) (conj new-params (first params)) lets)
(let [gparam (gensym "p__")]
(recur (next params) (conj new-params gparam)
(-> lets (conj (first params)) (conj gparam)))))
`(~new-params
(let ~lets
~@body))))))

;redefine fn with destructuring and pre/post conditions
(defmacro fn
"(fn name? [params* ] exprs*)
Expand Down Expand Up @@ -3077,20 +3095,7 @@
(concat (map (fn* [c] `(assert ~c)) pre)
body)
body)]
(if (every? symbol? params)
(cons params body)
(loop [params params
new-params []
lets []]
(if params
(if (symbol? (first params))
(recur (next params) (conj new-params (first params)) lets)
(let [gparam (gensym "p__")]
(recur (next params) (conj new-params gparam)
(-> lets (conj (first params)) (conj gparam)))))
`(~new-params
(let ~lets
~@body)))))))
(maybe-destructured params body)))
new-sigs (map psig sigs)]
(with-meta
(if name
Expand Down
4 changes: 3 additions & 1 deletion src/clj/clojure/core_deftype.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
set
(disj 'Object 'java.lang.Object)
vec)
methods (apply concat (vals impls))]
methods (map (fn [[name params & body]]
(cons name (maybe-destructured params body)))
(apply concat (vals impls)))]
(when-let [bad-opts (seq (remove #{:no-print} (keys opts)))]
(throw (IllegalArgumentException. (apply print-str "Unsupported option(s) -" bad-opts))))
[interfaces methods opts]))
Expand Down

0 comments on commit bf8bb79

Please sign in to comment.