tags:

views:

941

answers:

2

The use of symbol literals is not immediately clear from what I've read up on Scala. Would anyone care to share some real world uses?

Is there a particular Java idiom being covered by symbol literals? What languages have similar constructs? I'm coming from a Python background and not sure there's anything analogous in that language.

What would motivate me to use 'HelloWorld vs "HelloWorld"?

Thanks

+6  A: 

If you have plain strings representing say method names in code, that perhaps get passed around, you're not quite conveying things appropriately. This is sort of the Data/Code boundary issue, it's not always easy to the draw the line, but if we were to say that in that example those method names are more code than they are data, then we want something to clearly identify that.

A Symbol Literal comes into play where it clearly differentiates just any old string data with a construct being used in the code. It's just really there where you want to indicate, this isn't just some string data, but in fact in some way part of the code. The idea being things like your IDE would highlight it differently, and given the tooling, you could refactor on those, rather than doing text search/replace.

This link discusses it fairly well.

Saem
+11  A: 

In Java terms, symbols are interned strings. This means, for example, that equality comparison works correctly: 'abcd == 'abcd will return true, while "abcd" == "abcd" might not (depending on JVM's whims).

Other languages which use symbols are Lisp (which uses 'abcd like Scala), Ruby (:abcd), Erlang and Prolog (abcd; they are called atoms instead of symbols).

I would use a symbol when I don't care about the structure of a string and use it purely as a name for something. For example, if I have a database table representing CDs, which includes a column named "price", I don't care that the second character in "price" is "r", or about concatenating column names; so a database library in Scala could reasonably use symbols for table and column names.

Alexey Romanov
It's probably worth remembering that == in Scala does the .equals thing, so really the difference would be when using the "eq" method which does reference equality. One bonus, though, is that comparison between symbols is extremely cheap.
Calum
@Calum, as is comparison between two strings. Java interns (more or less) all strings.
Elazar Leibovich
@Elazar: Is that really true? I was under the impression that Java only interned literals (i.e. almost all strings in trivial examples, and almost no strings in production software). Having said the use-cases of symbols are usually as literal values (I doubt you often build them from scratch), so arguably the main advantage you get is just a more descriptive type.
Calum
@Elazar Actually, it occurs to me now that even in the case of an interned Java String, you only benefit from interning when comparing two identical objects. Since the runtime cannot guarantee that two instances will both be interned, all interning yields is an early-out in the case where the items are the same. It does not bypass the requirement to check character-by-character on Strings of equal length.
Calum
@Elazar Sorry for keeping updating this same answer (there's a time limit!). I've elaborated on this point in the answer to another question: http://stackoverflow.com/questions/3554362/purpose-of-scalas-symbol/3555381#3555381 . For what it's worth, though, I'm certain that Java does not intern dynamically-created Strings.
Calum
@Calum, indeed Java doesn't intern dynamically created strings. But Symbols are equivalent to Java's string literals, not to just any string. See http://javatechniques.com/public/java/docs/basics/string-equality.html
Elazar Leibovich
@Elazar They are not, because Symbols can rely upon their value being interned, whereas with Strings its optional and can optimise some use-cases. Symbols are more like dynamically-created Enums than anything. I elaborated on this on the answer I linked in my last response; please read it for more.
Calum