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

查看:110
本文介绍了什么是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

表单将打开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天全站免登陆