Scala 中的 _root_ 包是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/687071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the _root_ package in Scala?
提问by Landon Kuhn
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:
我将 IntelliJ IDEA 与 Scala 插件一起使用。如果我在代码中引用 HashMap,然后使用 Alt-Enter 添加导入,则包将导入为:
_root_.scala.collection.immutable.HashMap
What's the rootpart of this? It seems to work with and without it.
这其中的根源是什么?它似乎可以使用和不使用它。
回答by Kevin Hakanson
It has to do scala imports being relative - _root_gives you a way to specify an absolute package name. See the Scala Wiki
它必须做相对的 Scala 导入 -_root_为您提供一种指定绝对包名称的方法。参见Scala 维基
回答by Alan LaMielle
The Scala language specification has this to say about _root_in section 9.4 Package References
Scala 语言规范_root_在第 9.4 节“包参考”中对此进行了说明
The special prede?ned name
_root_refers to the outermost root package which contains all top-level packages.
特殊的预定义名称
_root_是指包含所有顶级包的最外层根包。
See the following PDF for the full language reference: http://www.scala-lang.org/docu/files/ScalaReference.pdf
有关完整的语言参考,请参阅以下 PDF:http: //www.scala-lang.org/docu/files/ScalaReference.pdf
回答by starblue
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.
只有在当前包中有一个包含 HashMap 的嵌套包 scala.collection.immutable 时,才需要它。这将是没有 _root_ 部分的相对导入的首选。
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.
编辑:这不太正确,如果您有一个 scala 包作为祖先或嵌套在当前包中,问题就已经开始了。

