需要澄清 Scala 文字标识符(反引号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6576594/
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
Need clarification on Scala literal identifiers (backticks)
提问by mythicalprogrammer
Reading the Programming in Scala 2nd Ed and I came across this:
阅读 Scala 编程第 2 版,我发现了这个:
literal identifier "The idea is that you can put any string that's accepted by the runtime as an identifier between backtick"
文字标识符“这个想法是你可以把运行时接受的任何字符串作为反引号之间的标识符”
I'm not entirely sure why I would use this? The book gave a use case of accessing the static yield method in Java's Thread class.
我不完全确定为什么要使用它?这本书给出了访问Java Thread 类中的静态yield 方法的用例。
So since in Scala, yield is a reserve word, if I use yield with backticks,
所以因为在 Scala 中,yield 是一个保留字,如果我使用带反引号的 yield,
Thread.`yield`()
it would ignore the Scala's yield and let me access the Java's Thread class's method yield instead?
它会忽略 Scala 的 yield 并让我访问 Java 的 Thread 类的方法 yield 吗?
Thank you in advance.
先感谢您。
回答by Debilski
Exactly. Using backticks, you can more or less give any name to a field identifier. In fact, you can even say
确切地。使用反引号,您可以或多或少地为字段标识符命名。事实上,你甚至可以说
val ` ` = 0
which defines a variable with name (one character of whitespace).
它定义了一个带有名称的变量(一个空格字符)。
The literal definition of identifiers is useful in two cases. The first case is, when there is already a reserved word of the same name in Scala and you need to use a Java library which does not care about that (and of course, why should it).
标识符的字面定义在两种情况下很有用。第一种情况是,当 Scala 中已经有一个同名的保留字并且您需要使用一个不关心它的 Java 库时(当然,为什么要这样做)。
The other use case comes with casestatements. The convention is that lower case names refer to match variables, whereas upper case names refer to identifiers from the outer scope. So,
另一个用例带有case语句。约定是小写名称指的是匹配变量,而大写名称指的是来自外部作用域的标识符。所以,
val A = "a"
val b = "b"
"a" match {
case b => println("b")
case A => println("A")
}
prints "b"(if the compiler were dumb enough not to fail with saying case Awere unreachable). If you want to refer to the originally defined val b, you need to use backticks as a marker.
打印"b"(如果编译器足够笨,不会因为无法case A访问而失败)。如果要引用最初定义的val b,则需要使用反引号作为标记。
"a" match {
case `b` => println("b")
case A => println("A")
}
Which prints "A".
哪个打印"A"。
AddThere is a more advanced use case in this recent question method with angle brackets (<>)where the backticks were needed to get the compiler to digesting the code for a setter method (which in itself uses some ‘magic' syntax).
添加在这个最近的带有尖括号 (<>) 的问题方法中有一个更高级的用例,其中需要反引号来让编译器消化 setter 方法的代码(它本身使用一些“魔术”语法)。
回答by Micka?l Gauvin
Thank you @Debilski, it helps me to understand this code below from AKKA doc :
谢谢@Debilski,它帮助我理解下面来自 AKKA doc 的代码:
class WatchActor extends Actor {
val child = context.actorOf(Props.empty, "child")
...
def receive = {
...
case Terminated(`child`) ? ...
}
}
The case :
案子 :
case Terminated(`child`)
matches a message of type Terminated with ActorRef field equals to child which is defined earlier.
匹配类型为 Terminated with ActorRef 字段的消息,该字段等于之前定义的 child。
With this statement :
有了这个声明:
case Terminated(c)
We match every Terminated messages with any reference of ActorRef mapped in c.
我们将每个 Terminated 消息与映射到c 中的 ActorRef 的任何引用进行匹配。

