Distinguishing a record instance from a map
clojure
Solution
Records seem to implement the IRecord marker interface:
user=> (defrecord Rec [x])
user.Rec
user=> (require '[clojure.reflect :as r])
nil
user=> (:bases (r/reflect Rec))
#{java.io.Serializable clojure.lang.IKeywordLookup clojure.lang.IPersistentMap clojure.lang.IRecord java.lang.Object clojure.lang.IObj clojure.lang.ILookup java.util.Map}
user=> (instance? clojure.lang.IRecord (Rec. "hi"))
true
Update 1.6 now has the `record?` functions
Problem
I'm trying to call `clojure.walk/stringify-keys` on a map that might include record instances. Since stringify-keys is recursive, it attempts to convert the keys on my record, (since `(map? record-var)` is true) which causes an error. Is there any way to tell if a var is a record rather than just a Clojure map? I d like to provide my own implementation of `stringify-keys` that is record-aware. The current implementation of `stringify-keys` causes the following: ``` (use '[clojure.walk :only [stringify-keys]]) (defrecord Rec [x]) (let [record (Rec. "foo") params {:x "x" :rec record}] (stringify-keys params)) ``` This causes the following exception: UnsupportedOperationException Can't create empty: user.Rec user.Rec (NO_SOURCE_FILE:1)