(def h (new java.util.IdentityHashMap))
(. h (put "a" "b"))
(println "a:" (. h (get "a")))
(. h (put :a :b))
(println "a:" (. h (get :a)))
prints:
a: nil
a: :b
Java guarantees that string literals and string-valued constants are interned:
import java.util.*;
public class Lit {
public static void main(String[] args) {
IdentityHashMap<String, String> hm = new IdentityHashMap<String, String>();
hm.put("a", "b");
System.out.println("a: " + hm.get("a"));
}
}
prints:
a: b
For interoperability with Java, and the principle of least surprise, I
think the Clojure reader should also intern string literals that it
reads.
Thanks,
Darshan
> I've restored string literal interning for compiled
> literals. Note however that this is not, and will not be, interning of
> strings in the reader. The reader can be used for arbitrary data
> reading, and it is inappropriate to intern every string read. Only
> string literals that are evaluated/compiled are interned.
That works great, thanks!
Cheers,
Darshan