weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

NPE from assoc-some if map is nil and there is more than one key supplied #73

Closed dmcgillen closed 1 year ago

dmcgillen commented 1 year ago

It used to be the case that the map passed to assoc-some could be nil, but the recent changes made to improve performance mean this now doesn't work if more than one key is supplied.

e.g.:

(medley/assoc-some nil :k "v")
;; => {:k "v"}

but

(medley/assoc-some nil :k1 "v1" :k2 "v2")
;; => java.lang.NullPointerException

I don't know if we're supposed to rely on the behaviour of assoc-some (or even Clojure core's assoc) when the supplied map is nil as it's not specifically documented from what I can see, but looking at the Java implementation of assoc it does specifically check for null and return a new map in that case. Obviously feel free to close this if you don't feel it's documented behaviour.

Thanks!

weavejester commented 1 year ago

Let's use the current behavior of assoc, and make it return a map when the first argument is nil.