Scala 2.8 中的嵌套/非嵌套包是什么? [英] What are nested/unnested packages in Scala 2.8?

查看:19
本文介绍了Scala 2.8 中的嵌套/非嵌套包是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 2.7 中,我可以这样写:

In Scala 2.7, I could write:

package com.acme.bar

class Bar

.

package com.acme.foo

class Foo {
  new bar.Bar
}

这在 Scala 2.8 中无法编译——但是可以:

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
}

  1. 这样做的动机是什么?
  2. 关于范围和可见性的确切含义是什么?
  3. 我什么时候应该使用一种形式而不是另一种形式?

推荐答案

关于这个问题在邮件列表上进行了几次长时间的讨论.请参阅 此主题的问题这个线程解决方案.

There were several long discussions on the mailing lists about this. See this thread for the problem and this thread for the solution.

至于意义,只有

package A
package B

form 将为 A 打开一个作用域,这使得 A 的成员可见而无需前缀.如果您的项目包含多个相互引用的子包,您通常会使用此表单.另一方面,您会使用表单

form will open a scope for A, which makes members of A 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 form

package A.B.C

如果您想将 C 集成到包层次结构中,并且不打算直接访问 AB 的其他成员.一个典型的案例是

If you want to integrate C into the package hierarchy and do not intend to access other members of A or B directly. A typical case is

package net.myorg.myproject

在这里,您不想比其他人定义的可能性更脆弱net.java 包会影响根级别的 java.在 Scala 2.7 中,您可以通过使用 _root_ 导入来防止这种情况.但这很丑陋,为了安全起见,您几乎必须在任何地方都这样做.所以目前的解决方案要好得多,IMO.

Here, 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.

这篇关于Scala 2.8 中的嵌套/非嵌套包是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆