Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cool class and method names wrapped in ``: class `This is a cool class` {}?

Tags:

scala

I just found some scala code which has a strange class name:

 class `This is a cool class` {}

and method name:

 def `cool method` = {}

We can use a sentence for a class or method name!

It's very cool and useful for unit-testing:

class UserTest {
   def `user can be saved to db` {
      // testing
   }
}

But why we can do this? How to understand it?

like image 687
Freewind Avatar asked May 29 '12 11:05

Freewind


2 Answers

This feature exists for the sake of interoperability. If Scala has a reserved word (with, for example), then you can still refer to code from other languages which use it as a method or variable or whatever, by using backticks.

Since there was no reason to forbid nearly arbitrary strings, you can use nearly arbitrary strings.

like image 142
Rex Kerr Avatar answered Nov 09 '22 13:11

Rex Kerr


As @Rex Kerr answered, this feature is for interoperablility. For example,

To call a java method,

Thread.yield()

you need to write

Thread.`yield`()

since yield is a keyword in scala.

like image 7
Prince John Wesley Avatar answered Nov 09 '22 15:11

Prince John Wesley