简体   繁体   中英

In clojure, what is assocEx for?

I am implementing a custom data structure, that, for all intents and purposes, is a sorted map. I wanted to program it so that I can use all of clojure's abstractions when it comes to maps, such as assoc , count , etc. In order to do that, I need to extend clojure's clojure.lang.IPersistentMap protocol to my new sorted map type.

My question is this: What does assocEx do, or what is its purpose? Calling it on a normal clojure map shows that it differs from assoc in that it throws an exception if you try to insert a key/value pair where the key already exists in the map:

user> (.assocEx (cast clojure.lang.IPersistentMap {:a :b}) :a 1)
RuntimeException Key already present  clojure.lang.Util.runtimeException (Util.java:219)

This is different from assoc , which simply replaces the old value in the map with the new one:

user> (.assoc (cast clojure.lang.IPersistentMap {:a :b}) :a 1)
{:a 1}

But I can't seem to find a "clojure" function (one where the interop form/cast is not used above) that displays this functionality. Where do we see the effects of this function using the normal clojure abstraction functions like assoc ?

Nothing ever uses assocEx anymore. It is a remnant of an earlier time. If you are writing your own map type, you can implement (assocEx [mkv] (throw (Exception.))) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM