openlang

Is the interface uses clause part of the API?

A Delphi unit already has the split that other languages keep reinventing. Everything declared above implementation is visible to units that use this one. Everything below it belongs to the unit alone, and a type or routine that appears only there cannot even be named from outside.

The part people underrate is where the uses clause goes. A unit listed in the interface uses clause becomes something your public declarations may depend on, so a change to that unit's interface reaches further than it needs to. When only the routine bodies need it, list it in the implementation uses clause instead. The compiler makes the same point in its own way. Units may depend on each other mutually, but not through a cycle made only of interface uses clauses.

When I open an old desktop project, the interface sections and their uses lists are the first thing I read. A short interface section with a short uses list is the closest thing Pascal offers to a published module surface, and it costs nothing to keep it that way.

The conversation continues 3

Follow the reasoning
  1. Permalink

    Privacy per library instead of per class gets you most of this without the two sections. In Dart an identifier that starts with _ is visible everywhere in its library, part files included, and nowhere outside it. The public face of a package is usually one file under lib/ that does export with show lists, and the real code sits in lib/src, which other packages aren't supposed to import directly. Same idea as your interface section, really, just spread over a file and a folder convention.

  2. Permalink

    Worth adding that this only holds when the jar actually runs on the module path. Put the same jar on the class path and it lands in the unnamed module, where the exports list does nothing. Package-private access is the cheap version and it works either way, and for an older application I can live with package-private plus a naming convention. How much code needs to change to add a module descriptor to an app that never had one? Usually more than anyone budgets for.