Using a Clojure 1.8.0 REPL, I get the following:
user=> {(+ 1 2) 1 (+ 1 2) 2}
IllegalArgumentException Duplicate key: (+ 1 2) clojure.lang.
PersistentArrayMap.createWithCheck (PersistentArrayMap.java:71)
It seems that the a check for key equality is made *before* the key is
evaluated, when they are still strings.
PersistentArrayMap/createWithCheck is as below:
static public PersistentArrayMap createWithCheck(Object[] init){
for(int i=0;i< init.length;i += 2)
{
for(int j=i+2;j<init.length;j += 2)
{
if(equalKey(init[i],init[j]))
throw new IllegalArgumentException("Duplicate key: " + init[i]);
}
}
return new PersistentArrayMap(init);
}
The MapReader in LispReader.java seems to pass an Object[] of Strings to
PersistentArrayMap/createWithCheck.
public static class MapReader extends AFn{
public Object invoke(Object reader, Object leftparen, Object opts, Object
pendingForms) {
PushbackReader r = (PushbackReader) reader;
Object[] a = readDelimitedList('}', r, true, opts,
ensurePending(pendingForms)).toArray();
if((a.length & 1) == 1)
throw Util.runtimeException("Map literal must contain an even number of
forms");
return RT.map(a);
}
}
This behavior is surprising. Is there a reason the map creation process
works this way?
The above example may seem trivial, since we would have duplicate keys
after evaluation as well, but consider the generation of random values:
user=> {(java.util.UUID/randomUUID) 1 (java.util.UUID/randomUUID) 2}
IllegalArgumentException Duplicate key: (java.util.UUID/randomUUID)
clojure.lang.PersistentArrayMap.createWithCheck
(PersistentArrayMap.java:71)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.