What is the _root_ package in Scala?

2019-01-18 08:26发布

I'm using IntelliJ IDEA with the Scala plugin. If I reference HashMap in code, and then use Alt-Enter to add the import, the package gets imported as:

_root_.scala.collection.immutable.HashMap

What's the root part of this? It seems to work with and without it.

3条回答
Luminary・发光体
2楼-- · 2019-01-18 08:52

It has to do scala imports being relative - _root_ gives you a way to specify an absolute package name. See the Scala Wiki

查看更多
你好瞎i
3楼-- · 2019-01-18 08:52

The Scala language specification has this to say about _root_ in section 9.4 Package References

The special predefined name _root_ refers to the outermost root package which contains all top-level packages.

See the following PDF for the full language reference: http://www.scala-lang.org/docu/files/ScalaReference.pdf

查看更多
贼婆χ
4楼-- · 2019-01-18 09:01

You would only need it if inside your current package you had a nested package scala.collection.immutable containing HashMap. This would be preferred by a relative import without the _root_ part.

Edit: That was not quite right, the problems start already if you have a scala package either as an ancestor or nested in the current package.

查看更多
登录 后发表回答