67

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

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.

So since in Scala, yield is a reserve word, if I use yield with backticks,

Thread.`yield`()

it would ignore the Scala's yield and let me access the Java's Thread class's method yield instead?

Thank you in advance.

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
mythicalprogrammer
  • 4,647
  • 6
  • 35
  • 42

2 Answers2

128

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).

The other use case comes with case statements. The convention is that lower case names refer to match variables, whereas upper case names refer to identifiers from the outer scope. So,

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 A were unreachable). If you want to refer to the originally defined val b, you need to use backticks as a marker.

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

Which prints "A".

Add There 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).

Community
  • 1
  • 1
Debilski
  • 66,976
  • 12
  • 110
  • 133
  • There also seems to be a special case for package objects. E.g. in `Predef.scala`, there is this line: ``scala.`package` // to force scala package object to be seen.`` Not sure it's a special case though… Any comment? – Jean-Philippe Pellet Jul 05 '11 at 07:21
  • Normally, you would not need to refer to a specific package object but just import whatever method you need from the package. In the Predef, I guess, this is a bit special because you explicitly want to expose those methods. Apart from that, it is not really special. It turns a keyword into an identifier just the same. – Debilski Jul 05 '11 at 13:35
  • Ok, maybe it’s not exposing but something else. – Debilski Jul 05 '11 at 15:16
  • 1
    You can also define a package object with: `object \`package\` {}` – retronym Feb 18 '12 at 10:13
  • It also allows one to have a script called `my-script.scala` with `object \`my-script\``. – Noel Yap Jul 19 '13 at 17:45
  • 2
    Another case where literal identifiers can be very useful: producing JSON with a Scala reserved word as field name (e.g. `{"type": "foo"}`) using Play's JSON API. http://stackoverflow.com/a/34018822/56285 – Jonik Dec 01 '15 at 11:54
20

Thank you @Debilski, it helps me to understand this code below from 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.

With this statement :

case Terminated(c)

We match every Terminated messages with any reference of ActorRef mapped in c.

Mickaël Gauvin
  • 607
  • 7
  • 15