Closed leaf packages

Packages are oftentimes used as ways to break down single code files. This can improve code organization, as it

  1. avoids long files.
  2. reduces the scope of top-level import statements.
  3. allows definitions to be grouped (and the latter to be named).
  4. reduces artificial sequentialization of definitions, which conceptually form an unordered set.

However, packages are currently treated quite differently from files by Scala, as, in general, they can span multiple directories, and do not form an atomic incremental compilation unit. This prevents sealed and wildcard exports from working at the package level.

Could a more restricted new type of package be introduced to Scala, to be treated by the compiler like a single file?

Such closed leaf packages could either be declared explicitly, or perhaps even be inferred by the compiler (though the latter might not be practical due to incremental compilation).

Possible restrictions:

  1. Single directory
  2. Same name as directory
  3. No sub-packages
  4. Bounded size
1 Like

There was a somewhat recent topic about refactoring a “file” to multiple files.

I have previously informally proposed taking a directory as a “compilation unit”.

I think with JDK “module” support there is more control over the package, which is no longer “open”. I have not considered what might happen to “subpackages” under that regime.

2 Likes