简体   繁体   中英

Scala: keyword as package name

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 the package name, so I can't import from it, and I can't refer to its classes with fully qualified name either.

Is there a way around it?

将对象包装在`((1旁边的引号)应该有效。

xxx.xxx.`object` 

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`()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM