What does package access solve?

1.7.3 · L3 · Interview

Mental model. internal is a room key. package is a building key. public is a key to the street.

In an SPM package with many targets, internal is per target. Shared helpers therefore had to be public, leaking them to everyone who imported the package.

package makes a declaration visible across all targets in the same package, but not to importers. It is the correct default for a multi-target app or library that is not meant to be a public API.

Common traps

  • Confusing package with module. A package contains modules (targets); a module is one compilation unit.
  • Assuming package is in the ABI. It is a source-level access level; it does not create a stable public interface.

They might follow up with

  • When would you still use public? Only for the package’s actual exported API — the product that clients import.
  • Does @testable import reach package? Test targets in the same package can use package directly; @testable is for internal.

Related questions

  • 1.7.1 What is the purpose of access control? List the levels.
  • 1.7.4 What are @usableFromInline and @inlinable in relation to access control?

← public vs open? Can a struct be open?   What are @usableFromInline and @inlinable in relation to access… →