String-literal interning?

146 views
Skip to first unread message

Darshan Shaligram

unread,
Mar 14, 2008, 4:34:19 AM3/14/08
to clo...@googlegroups.com
Clojure does not appear to intern string literals:

(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

Rich Hickey

unread,
Mar 14, 2008, 8:42:53 AM3/14/08
to Clojure


On Mar 14, 4:34 am, "Darshan Shaligram" <scinti...@gmail.com> wrote:
> Clojure does not appear to intern string literals:
>

> For interoperability with Java, and the principle of least surprise, I
> think the Clojure reader should also intern string literals that it
> reads.
>

This was a bit of regression when I switched to different constant
handling. 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.

Rich

Darshan Shaligram

unread,
Mar 14, 2008, 8:46:15 AM3/14/08
to clo...@googlegroups.com
On Fri, Mar 14, 2008 at 6:12 PM, Rich Hickey <richh...@gmail.com> wrote:
> On Mar 14, 4:34 am, "Darshan Shaligram" <scinti...@gmail.com> wrote:
> > Clojure does not appear to intern string literals:

> 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

Reply all
Reply to author
Forward
0 new messages