What are some example use cases for symbol literals in Scala?

SyntaxScala

Syntax Problem Overview


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

Syntax Solutions


Solution 1 - Syntax

In Java terms, symbols are interned strings. This means, for example, that reference equality comparison (eq in Scala and == in Java) gives the same result as normal equality comparison (== in Scala and equals in Java): 'abcd eq 'abcd will return true, while "abcd" eq "abcd" might not, depending on JVM's whims (well, it should for literals, but not for strings created dynamically in general).

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.

Solution 2 - Syntax

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.

Solution 3 - Syntax

Note: Symbols will be deprecated and then removed in Scala 3 (dotty).

Reference: http://dotty.epfl.ch/docs/reference/dropped-features/symlits.html

Because of this, I personally recommend not using Symbols anymore (at least in new scala code). As the dotty documentation states:

> Symbol literals are no longer supported

> it is recommended to use a plain string literal [...] instead

Solution 4 - Syntax

Python mantains an internal global table of "interned strings" with the names of all variables, functions, modules, etc. With this table, the interpreter can make faster searchs and optimizations. You can force this process with the intern function (sys.intern in python3).

Also, Java and Scala automatically use "interned strings" for faster searchs. With scala, you can use the intern method to force the intern of a string, but this process don't works with all strings. Symbols benefit from being guaranteed to be interned, so a single reference equality check is both sufficient to prove equality or inequality.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe HollowayView Question on Stackoverflow
Solution 1 - SyntaxAlexey RomanovView Answer on Stackoverflow
Solution 2 - SyntaxSaemView Answer on Stackoverflow
Solution 3 - SyntaxjuanmirocksView Answer on Stackoverflow
Solution 4 - SyntaxChemaCortesView Answer on Stackoverflow