Scala 2.8 中的嵌套/非嵌套包是什么?
在 Scala 2.7 中,我可以这样写:
package com.acme.bar
class Bar
。
package com.acme.foo
class Foo {
new bar.Bar
}
这不能在 Scala 2.8 中编译——但是可以
package com.acme
package bar
class Bar
:
package com.acme
package foo
class Foo {
new bar.Bar
}
- 这样做的动机是什么?
- 关于范围和可见性,确切的含义是什么?
- 我什么时候应该使用一种形式而不是另一种形式?
In Scala 2.7, I could write:
package com.acme.bar
class Bar
.
package com.acme.foo
class Foo {
new bar.Bar
}
This doesn't compile in Scala 2.8 -- however this does:
package com.acme
package bar
class Bar
.
package com.acme
package foo
class Foo {
new bar.Bar
}
- What was the motivation for this?
- What is the precise meaning, with regards to scope and visibility?
- When should I use one form over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
邮件列表上对此进行了多次长时间的讨论。
请参阅此线程解决问题和此线程解决方案。
至于意义,只有
表单才会为
A
打开一个范围,这使得A
的成员在没有前缀的情况下可见。如果您的项目由多个相互引用的子包组成,您通常会使用此形式。另一方面,如果您想将
C
集成到包层次结构中,并且不打算访问A
或B 的其他成员,则 可以使用以下形式直接。一个典型的情况是
,您不希望容易受到其他人定义的可能性的影响
net.java 包将隐藏根级别 java。在 Scala 2.7 中,您可以通过使用
_root_
导入来防止这种情况发生。但这很丑陋,而且为了安全起见,你必须在几乎所有地方都这样做。所以目前的解决方案要好得多,IMO。There were several long discussions on the mailing lists about this.
See this thread for the problem and this thread for the solution.
As to meaning, only the
form will open a scope for
A
, which makes members ofA
visible without prefix. You'd typically use this form if your project consists of several subpackages which refer to each other. On the other hand, you'd use the formIf you want to integrate
C
into the package hierarchy and do not intend to access other members ofA
orB
directly. A typical case isHere, you do not want to be vulnerable to the possibility than somebody else has defined a
net.java package that would shadow the root level java. In Scala 2.7 you'd prevent that by using
_root_
imports. But that's ugly, and to be safe you'd have to do this almost everywhere. So the present solution is much better, IMO.感谢到目前为止的回答!让我补充两个小点,我们就完成了!
可见
性 嵌套包和非嵌套包之间的区别仅适用于范围界定。 可见性始终基于嵌套包。
这有效:
这样做也是如此:
在这两种情况下,结构都被解释为:
作用域
将此与作用域 进行比较,您可以想象对未嵌套包的这种解释:
混合和匹配
您可以任意混合和匹配嵌套和非嵌套包:
Thanks for the answers so far! Let me add two small points, and we're done!
Visibility
The difference between nested and unnested packages only applies to scoping. Visibility is always based on nested packages.
This works:
So does this:
In both cases, the structure is interpreted as:
Scoping
Compare this with scoping, in which you could imagine this interpretation for unnested packages:
Mix and Match
You can arbitrarily mix and match nested and unnested package:
它不会让您更好地控制导入的内容吗?例如,如果有包:
然后从
Foo
内部,是否存在关于引用哪个client
的恼人的歧义?例如,如果您想从Foo
中进行通配符导入:如果我们使用
client
而不是client
,这可能会产生更多问题一个包com.acme.java
:Doesn't it give you more control over what is being imported? For example, if there were packages:
And then from within
Foo
, wasn't there an annoying ambiguity about whichclient
was being referred to? For example, if you want to do a wildcard import from withinFoo
:This might be much more problematic if, instead of
client
, we had a packagecom.acme.java
: