Error formatting macro: pagetree: java.lang.NullPointerException
Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

The expectation is that this will become a JIRA ticket in the future, after the details are more fleshed out.

See this thread on the Clojure Google group: https://groups.google.com/forum/?fromgroups=#!topic/clojure/AG667ACBd3I

Note especially Chas Emerick's detailed analysis of how we arrived at the current state, posted Aug 5, 2012.

Current behavior of Clojure 1.4.0:

;; Sets
;; There might not be much objection to this behavior, with identical constants:
user=> #{28 28}
IllegalArgumentException Duplicate key: 28  clojure.lang.PersistentHashSet.createWithCheck (PersistentHashSet.java:68)

;; It is when set literals contain variables that are unexpectedly equal
;; that some do not want an exception thrown, but perhaps merely a
;; warning would be better.
user=> (def a 28)
#'user/a
user=> (def b 28)
#'user/b
IllegalArgumentException Duplicate key: 28  clojure.lang.PersistentHashSet.createWithCheck (PersistentHashSet.java:68)

;; This is one way to construct a set that allows duplicates.
user=> (set [a b])
#{28}


;; Maps

;; Similar to sets, except that only keys must be distinct.
;; However, in this case the construction functions array-map
;; and hash-map also disallow duplicate keys, whereas
;; sorted-map permits them.

user=> {a 5 b 7}
IllegalArgumentException Duplicate key: 28  clojure.lang.PersistentArrayMap.createWithCheck (PersistentArrayMap.java:70)
user=> (array-map a 5 b 7)
IllegalArgumentException Duplicate key: 28  clojure.lang.PersistentArrayMap.createWithCheck (PersistentArrayMap.java:70)
user=> (hash-map a 5 b 7)
IllegalArgumentException Duplicate key: 28  clojure.lang.PersistentHashMap.createWithCheck (PersistentHashMap.java:92)
user=> (sorted-map a 5 b 7)
{28 7}

;; assoc is one way to create a map that silently eliminates duplicate keys
user=> (assoc {} a 5 b 7)
{28 7}

 

Possible ideas for changes to the current behavior.

  1. Revert to previous behavior where literal sets and maps always quietly eliminate duplicate elements/keys, even in cases like #{5 5} or {:a 1 :b 2}.
  2. Quietly eliminate duplicates if they are due to non-constant subexpressions, e.g. #{foo bar}, but error for duplicate constant subexpressions.
  3. Add compile time option to choose between current error, warning, or quietly eliminating duplicates.
Labels: