Search code examples
scalapackageaccess-modifiers

How do I make a package in Scala private?


If I have a package hierarchy in Scala like this:

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
  }
}

how can I forbid the users of package toplevel to see or import package utility?

I tried with private[toplevel] package utility { ... but got this error: expected start of definition.

I've searched for this and was overwhelmed by false positives: everything I got was about making things package-private and this is not my question.


Solution

  • You can't: packages don't have access levels.

    Or rather, you can, by using OSGi and not exporting them, but this is a very heavy-weight solution if you don't need it for some other reason as well.