Scala 中符号文字的一些示例用例是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/780287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:25:29  来源:igfitidea点击:

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

syntaxscala

提问by Joe Holloway

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?

从我在 Scala 上读到的内容来看,符号文字的使用并不是很清楚。有人愿意分享一些现实世界的用途吗?

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.

符号文字是否涵盖了特定的 Java 习语?哪些语言有类似的结构?我来自 Python 背景,不确定该语言中是否有类似的东西。

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

什么会促使我使用“HelloWorld 与“HelloWorld”?

Thanks

谢谢

采纳答案by Alexey Romanov

In Java terms, symbols are interned strings. This means, for example, that reference equality comparison (eqin Scala and ==in Java) gives the same result as normal equality comparison (==in Scala and equalsin Java): 'abcd eq 'abcdwill 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).

在 Java 术语中,符号是内部字符串。这意味着,例如,引用相等比较(eq在 Scala 和==Java 中)给出与正常相等比较(==在 Scala 和equalsJava 中)相同的结果:'abcd eq 'abcd将返回 true,而"abcd" eq "abcd"可能不会,这取决于 JVM 的奇思妙想(好吧,它应该用于文字,但不适用于一般动态创建的字符串)。

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

其他使用符号的语言是 Lisp(使用'abcd如 Scala)、Ruby ( :abcd)、 Erlang 和 Prolog ( abcd;它们被称为原子而不是符号)。

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.

当我不关心字符串的结构并将其纯粹用作某物的名称时,我会使用符号。例如,如果我有一个表示 CD 的数据库表,其中包含一个名为“price”的列,我不在乎“price”中的第二个字符是“r”,或者关于连接列名;因此 Scala 中的数据库库可以合理地使用表名和列名的符号。

回答by Saem

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.

符号文字开始发挥作用,它清楚地将任何旧字符串数据与代码中使用的构造区分开来。它确实在您想要指示的地方,这不仅仅是一些字符串数据,而且实际上在某种程度上是代码的一部分。像您的 IDE 这样的想法会以不同的方式突出显示它,并且给定工具,您可以重构这些,而不是进行文本搜索/替换。

This linkdiscusses it fairly well.

这个链接很好地讨论了它。

回答by ChemaCortes

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 internfunction (sys.internin python3).

Python 维护着一个包含所有变量、函数、模块等名称的“内部字符串”的内部全局表。有了这个表,解释器可以进行更快的搜索和优化。您可以使用intern函数(sys.intern在 python3 中)强制执行此过程。

Also, Java and Scala automatically use "interned strings"for faster searchs. With scala, you can use the internmethod 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.

此外,Java 和 Scala 会自动使用“内部字符串”来加快搜索速度。使用scala,您可以使用该intern方法强制字符串的实习生,但此过程不适用于所有字符串。符号受益于保证被实习,因此单个引用相等性检查就足以证明相等或不相等。