5

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.

nedim
  • 1,767
  • 1
  • 18
  • 20
  • Why do you need that utility package? Wouldn't a private object be more suitable there? – znurgl Dec 17 '15 at 09:25
  • 1
    I need the package to put some meta stuff that simplifies development of the other packages. Such stuff should probably be in a separate library but we don't want the extra dependencies. I'd like it to be a package because it contains 6 objects/classes already with probably more to come. Putting them all in one object would mean that they have to be in the same file and that would become too messy. – nedim Dec 17 '15 at 09:40

3 Answers3

4

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.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
-1

To reach the same goal as private packages in Java you can use augmented access modifiers. Inside your package utility you restrict access with private[utility]. This way the package member is available only inside the package utility itself.

Hope that helps.

  • The question is how to make the entire package private, not how to make individual classes package-private. – nedim Dec 17 '15 at 10:47
-2
    You can only define the enclosing package, within which the code is defined

Answered here.

Community
  • 1
  • 1
Jet
  • 3,018
  • 4
  • 33
  • 48
  • The question is how to make the entire package private, not how to make individual classes package-private. – nedim Dec 17 '15 at 10:47