Scala has symbols - names that start with a single quote \' and which are a kind of string constants.
I know symbols from Ruby (where they start with a colon). In Ru
According to the Scala book, Symbols are interned: "If you write the same symbol twice, both expressions will refer to the exact same Symbol
object."
In contrast, String
s are only interned if they appear in literal form (at least in Java they are, not entirely sure about Scala). So I guess if you do a lot of serialization of String
s that are then put into collections, you might use symbols instead and save yourself some memory.
But I agree with skaffman, I'm not totally convinced of their use.
(In Ruby, Symbol
s are, apart from the meta-programming example you give, often used as keys in Hash
es. In Ruby this is useful because there, String
s are never interned: every String
allocates new memory. In Scala it might be useful, as I mentioned, if you combine it with a lot of (de)serialization so the Java String
s don't get interned as well.)