[plt-scheme] immutable strings vs. uninterned symbols

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jun 6 09:16:55 EDT 2006

At Tue, 6 Jun 2006 08:52:07 -0400, Doug Orleans wrote:
> What's the difference between immutable strings and uninterned
> symbols? 

Besides the printing and reading conventions, immutable strings support
`string-ref' to access individual characters.

Matthias points out that strings support `string-append', too. A minor
complaint is that `string-append' produces mutable strings, not
immutable strings. You can use `string->immutable-string' on the
result, but you could also use `string->uninterned-symbol'. Making all
strings immutable is probably a good idea, and then Matthias would be
completely right.

> I mean, I realize they're different types, but they're both
> strings that can't be changed and are only eq? to themselves. 

In this way, aren't integers (including bignums) also like immutable
strings and uninterned symbols? The difference is that the internal
representation of integers is optimized for operations like `+' instead
of operations like `string-ref'.

> Is there any reason to prefer one over the other? 

If you want `string-ref', then uninterned strings are better (just like
you'd use integers if you want `+').

Offhand, I can't think of any other reason. I'm inclined to avoid
uninterned symbols, though, probably because I expect symbols to be
interned (more than I expect strings to be mutable).

> Which one is more like java.lang.String?

I think immutable strings are a little close to java.lang.String.


Posted on the users mailing list.