如何在Scala中创建包私有?

如果我在 Scala中有一个包层次结构,如下所示:

package toplevel {
  package a {
    // some interesting stuff
  }

  package b {
    // more interesting stuff
  }

  package utility {
    // stuff that should not be accessible from the outside
    // and is not logically related to the project,
    // basically some helper objects
  }
}

我怎样才能禁止包的顶层用户查看或导入包实用程序?

我尝试使用private [toplevel]包实用程序{…但是得到了这个错误:预期的定义开始.

我已经搜索过这个并被误报所淹没:我得到的一切都是关于将包装私有化,这不是我的问题.

最佳答案 你不能:包没有访问级别.

或者更确切地说,你可以使用OSGi而不是导出它们,但如果你因为某些其他原因不需要它,这是一个非常重量级的解决方案.

点赞