如何在 Scala 中将包设为私有?

ned*_*dim 5 scala access-modifiers package

如果我在 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
  }
}
Run Code Online (Sandbox Code Playgroud)

如何禁止包的用户toplevel查看或导入包utility

我尝试过private[toplevel] package utility { ...但收到此错误:expected start of definition

我已经搜索过这个,并被误报所淹没:我得到的一切都是关于使事情成为包私有的,这不是我的问题。

Ale*_*nov 4

你不能:包没有访问级别。

或者更确切地说,您可以通过使用OSGi并且不导出它们,但如果您由于其他原因也不需要它,那么这是一个非常重量级的解决方案。