Reputation: 12682
assoc
may throw IndexOutOfBoundsException when the index is, well, out of bounds, as in:
user=> (assoc [] 1 nil)
IndexOutOfBoundsException clojure.lang.PersistentVector.assocN(PersistentVector.java:137)
Why wouldn't it throw that same exception if I try to set the value at index 0?
user=> (assoc [] 0 nil)
[nil]
It seems to me that in both cases the index is out of bounds ...
Thanks
Upvotes: 4
Views: 174
Reputation: 9266
Assoc-in uses assoc to modify the element at the supplied key (index in your case). Assoc-in (and assoc) try to create what keys (or indexes) you require.
The special treatment for vectors is mentioned in the assoc docstring:
When applied to a vector, returns a new vector that contains val at index. Note - index must be <= (count vector).
Update: Just to clarify: The missing exception is consistent because while 0 is a valid index for a new element in an empty vector, 1 is not.
Upvotes: 3