1.7 · Access control
Access control is API design enforced by the compiler. The interesting levels are not private — they are package, public, and open, which decide what a binary can promise to the outside world.
Mental model. Pick the narrowest level that still lets the code that needs it compile. Widening later is easy; narrowing is a breaking change.
The access levels
Who can see a declaration.
- What is the purpose of access control? List the levels.
Access control limits who can depend on a declaration — it is how you keep an API small while keeping implementation details free to change. private — the… publicvsopen? Can a struct beopen?
open is for classes and class members that may be subclassed or overridden outside the module. public is visible to importers but closed to inheritance from…
Modules and packages
Boundaries bigger than a file.
- What does
packageaccess solve?
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.… - What are
@usableFromInlineand@inlinablein relation to access control?
@inlinable puts a function's body into the client's module so it can be specialized and inlined across module boundaries. The body may only reference public or…
More in Swift language
- 1.1 · Value vs reference types
- 1.2 · Optionals, pattern matching, control flow
- 1.3 · Protocols, extensions, associated types
- 1.4 · Generics, <code>some</code>, <code>any</code>
- 1.5 · Closures, escaping, capture lists
- 1.6 · Properties, observers, wrappers
- 1.8 · Error handling, <code>defer</code>, ownership
- 1.9 · Strings and Unicode
- 1.10 · Codable, Sendable, macros, ABI
- 1.11 · Static vs dynamic dispatch