diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-23 13:47:44 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-24 14:28:40 -0400 |
commit | e0e0b6a2f192bf743e3629dff0b23a39ccf4f8db (patch) | |
tree | 8e48cd72ce7b52445bdfd79db5e19ed14d8d76e8 /src | |
parent | d5578ae81ee920341a62d10f66744600ea832c48 (diff) |
make defrecord .cons work, #231
- based on original patch from Allen Rohner
- altered to handle nil correctly
- added test cases
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/core_deftype.clj | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/clj/clojure/core_deftype.clj b/src/clj/clojure/core_deftype.clj index ce81b448..6e1d8d7e 100644 --- a/src/clj/clojure/core_deftype.clj +++ b/src/clj/clojure/core_deftype.clj @@ -107,6 +107,22 @@ (defn munge [s] ((if (symbol? s) symbol str) (clojure.lang.Compiler/munge (str s)))) +(defn- imap-cons + [#^IPersistentMap this o] + (cond + (instance? java.util.Map$Entry o) + (let [#^java.util.Map$Entry pair o] + (.assoc this (.getKey pair) (.getValue pair))) + (instance? clojure.lang.IPersistentVector o) + (let [#^clojure.lang.IPersistentVector vec o] + (.assoc this (.nth vec 0) (.nth vec 1))) + :else (loop [this this + o o] + (if (seq o) + (let [#^java.util.Map$Entry pair (first o)] + (recur (.assoc this (.getKey pair) (.getValue pair)) (rest o))) + this)))) + (defn- emit-defrecord "Do not use this directly - use defrecord" [tagname name fields interfaces methods] @@ -163,7 +179,7 @@ (conj m `(count [~'this] (+ ~(count base-fields) (count ~'__extmap))) `(empty [~'this] (throw (UnsupportedOperationException. (str "Can't create empty: " ~(str classname))))) - `(cons [~'this ~'e] (let [[~'k ~'v] ~'e] (.assoc ~'this ~'k ~'v))) + `(cons [~'this ~'e] ((var imap-cons) ~'this ~'e)) `(equiv [~'this ~'o] (.equals ~'this ~'o)) `(containsKey [~'this ~'k] (not (identical? ~'this (.valAt ~'this ~'k ~'this)))) `(entryAt [~'this ~'k] (let [~'v (.valAt ~'this ~'k ~'this)] |