Unhelpful casting errors
WhittlesJr opened this issue · comments
I frequently encounter this, and I'm struggling to pinpoint the cause:
ClassCastException class robertluo.fun_map.core.WrappedEntry cannot be cast to class clojure.lang.MapEntry (robertluo.fun_map.core.WrappedEntry is in unnamed module of loader clojure.lang.DynamicClassLoader @5ab59675; clojure.lang.MapEntry is in unnamed module of loader 'app')
robertluo.fun-map.core.WrappedEntry (core.clj:37)
robertluo.fun-map.core.WrappedEntry (core.clj:49)
clojure.lang.RT.nthFrom (RT.java:976)
clojure.lang.RT.nth (RT.java:940)
robertluo.fun-map/touch (fun_map.clj:38)
robertluo.fun-map/touch (fun_map.clj:35)
clojure.core/comp/fn--5825 (core.clj:2573)
Is this a bug? If not, is there any way to improve the traceback? None of the code in the traceback is mine.
Here it is again. This time I can see where it's coming from but I can't see why:
ClassCastException class robertluo.fun_map.core.WrappedEntry cannot be cast to class clojure.lang.MapEntry (robertluo.fun_map.core.WrappedEntry is in unnamed module of loader clojure.lang.DynamicClassLoader @3e80e7d3; clojure.lang.MapEntry is in unnamed module of loader 'app')
robertluo.fun-map.core.WrappedEntry (core.clj:45)
robertluo.fun-map.core.DelegatedMap (core.clj:83)
clojure.lang.RT.get (RT.java:760)
rh.utils.maputils/merge-with-k/merge-entry--160026 (maputils.clj:35)
clojure.core.protocols/fn--8181 (protocols.clj:168)
clojure.core.protocols/fn--8181 (protocols.clj:124)
clojure.core.protocols/fn--8136/G--8131--8145 (protocols.clj:19)
clojure.core.protocols/seq-reduce (protocols.clj:31)
clojure.core.protocols/fn--8166 (protocols.clj:75)
clojure.core.protocols/fn--8166 (protocols.clj:75)
clojure.core.protocols/fn--8110/G--8105--8123 (protocols.clj:13)
clojure.core/reduce (core.clj:6830)
Where merge-with-k
is just a slightly altered version of clojure.core's merge-with
:
(defn merge-with-k
"Alters core logic to also pass the keyword to the function"
{:static true}
[f & maps]
(when (some identity maps)
(let [merge-entry (fn [m e]
(let [k (key e) v (val e)]
(if (and (map? m) (contains? m k))
(assoc m k (f k (get m k) v))
(assoc m k v))))
merge2 (fn [m1 m2]
(reduce merge-entry (or m1 {}) (seq m2)))]
(reduce merge2 maps))))