Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf8bb79

Browse files
committed
added parameter destructuring support to reify and deftype/record
1 parent db3466e commit bf8bb79

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/clj/clojure/core.clj

+19-14
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,24 @@
30433043
(even? (count bindings)) "an even number of forms in binding vector")
30443044
`(let* ~(destructure bindings) ~@body))
30453045

3046+
(defn ^{:private true}
3047+
maybe-destructured
3048+
[params body]
3049+
(if (every? symbol? params)
3050+
(cons params body)
3051+
(loop [params params
3052+
new-params []
3053+
lets []]
3054+
(if params
3055+
(if (symbol? (first params))
3056+
(recur (next params) (conj new-params (first params)) lets)
3057+
(let [gparam (gensym "p__")]
3058+
(recur (next params) (conj new-params gparam)
3059+
(-> lets (conj (first params)) (conj gparam)))))
3060+
`(~new-params
3061+
(let ~lets
3062+
~@body))))))
3063+
30463064
;redefine fn with destructuring and pre/post conditions
30473065
(defmacro fn
30483066
"(fn name? [params* ] exprs*)
@@ -3077,20 +3095,7 @@
30773095
(concat (map (fn* [c] `(assert ~c)) pre)
30783096
body)
30793097
body)]
3080-
(if (every? symbol? params)
3081-
(cons params body)
3082-
(loop [params params
3083-
new-params []
3084-
lets []]
3085-
(if params
3086-
(if (symbol? (first params))
3087-
(recur (next params) (conj new-params (first params)) lets)
3088-
(let [gparam (gensym "p__")]
3089-
(recur (next params) (conj new-params gparam)
3090-
(-> lets (conj (first params)) (conj gparam)))))
3091-
`(~new-params
3092-
(let ~lets
3093-
~@body)))))))
3098+
(maybe-destructured params body)))
30943099
new-sigs (map psig sigs)]
30953100
(with-meta
30963101
(if name

src/clj/clojure/core_deftype.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
set
5151
(disj 'Object 'java.lang.Object)
5252
vec)
53-
methods (apply concat (vals impls))]
53+
methods (map (fn [[name params & body]]
54+
(cons name (maybe-destructured params body)))
55+
(apply concat (vals impls)))]
5456
(when-let [bad-opts (seq (remove #{:no-print} (keys opts)))]
5557
(throw (IllegalArgumentException. (apply print-str "Unsupported option(s) -" bad-opts))))
5658
[interfaces methods opts]))

0 commit comments

Comments
 (0)