Need to refine Scala literal identifiers (backticks)

Reading programming in Scala 2nd Ed and I came across this:

literal identifier "The idea is that you can put any line that is accepted at run time as an identifier between the return stroke"

I'm not quite sure why I will use this? The book gave an example of using the static lesson method in the Java Thread class.

So, since in Scala, output is a backup word, if I use return windows

Thread.`yield`() 

would he ignore Scala's output and give me access to the Java Thread class method lesson instead?

Thanks in advance.

+47
scala
Jul 04 2018-11-22T00:
source share
2 answers

That's right. Using the return outputs, you can more or less give a name to the field identifier. In fact, you can even say

 val ` ` = 0 

which defines a variable named (one space character).

The literal definition of identifiers is useful in two cases. In the first case, when Scala has already reserved one word with the name Scala, and you need to use the Java library, which does not care about this (and, of course, why it should).

Otherwise, the case statement is used. The convention is that lowercase names refer to matching variables, while uppercase names refer to identifiers from the outer scope. In this way,

 val A = "a" val b = "b" "a" match { case b => println("b") case A => println("A") } 

prints "b" (if the compiler was dumb enough not to fail, saying that case A were unavailable). If you want to refer to the originally defined val b , you need to use backlinks as a marker.

 "a" match { case `b` => println("b") case A => println("A") } 

What prints "A" .

Add . This recent question has a more advanced use case with angle brackets (<gt;) , where backlinks were necessary for the compiler to digest the code for the setter method (which itself uses some kind of “magic syntax”).

+89
Jul 04 2018-11-22T00:
source share

Thanks @Debilski, it helps me understand this code below from the AKKA document:

 class WatchActor extends Actor { val child = context.actorOf(Props.empty, "child") ... def receive = { ... case Terminated(`child`) ⇒ ... } } 

Happening:

 case Terminated(`child`) 

matches a message of type Terminated with ActorRef, which is equal to the child that was previously defined.

With this statement:

 case Terminated(c) 

We map each completed message to any ActorRef link displayed in c .

+12
Aug 22
source share



All Articles