diff options
author | Rich Hickey <richhickey@gmail.com> | 2010-04-27 07:59:53 -0400 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2010-04-27 07:59:53 -0400 |
commit | bf8bb79ca64f6d29ca0dff3d40d18fe6568d558a (patch) | |
tree | 79ffab3733b6260bc086ecb7afc3677256b4bc9e | |
parent | db3466e80b74b3a640b11ab287daca1e5b7d12b4 (diff) |
added parameter destructuring support to reify and deftype/record
-rw-r--r-- | src/clj/clojure/core.clj | 33 | ||||
-rw-r--r-- | src/clj/clojure/core_deftype.clj | 4 |
2 files changed, 22 insertions, 15 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index 7ce2573e..3895c1d7 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -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*) @@ -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 diff --git a/src/clj/clojure/core_deftype.clj b/src/clj/clojure/core_deftype.clj index 63c310ef..58a181c0 100644 --- a/src/clj/clojure/core_deftype.clj +++ b/src/clj/clojure/core_deftype.clj @@ -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])) |