Scala: keyword as package name

前端 未结 2 455
孤街浪徒
孤街浪徒 2021-01-07 19:47

I\'m trying to use a Java library (no source code available) which defines some xxx.xxx.object package. Scala complains about the presence of \"object\" in

相关标签:
2条回答
  • 2021-01-07 20:49

    Wrapping the object in a ` (the quote next to 1) should work.

    xxx.xxx.`object` 
    
    0 讨论(0)
  • 2021-01-07 20:51

    To complete agilefall's answer, the Scala Language Specification mentions that an import is composed of id:

    id ::= plainid
    | ‘\`’ stringLit ‘\`’
    

    an identifier may also be formed by an arbitrary string between back-quotes (host systems may impose some restrictions on which strings are legal for identifiers). The identifier then is composed of all characters excluding the backquotes themselves.

    Backquote-enclosed strings are a solution when one needs to access Java identifiers that are reserved words in Scala.
    For instance, the statement Thread.yield() is illegal, since yield is a reserved word in Scala. However, here’s a work-around:

    Thread.`yield`()
    
    0 讨论(0)
提交回复
热议问题