skip to content

A Swift Core framework declares `public struct Session`, yet the Feature module cannot create a Session or read its properties; why, and what fixes it?

level: seniorimportance: should knowfreq 50%

answer

  1. public is opt-in member by member
  2. implicit initializers stay internal at most
  3. no declaration outranks its types
  4. tests can hide the bug

basics

~20 s

Members of a public type default to internal, and its implicit default and memberwise initializers are never public. Mark each member Feature needs as public and write an explicit public init; any type those members expose must be public too.

solid answer

~50 s

Making a type `public` does not publish its members: they default to `internal`, so Feature sees the type but gets `'token' is inaccessible due to 'internal' protection level`. The implicit initializers never rise above `internal`, so `Session(token:expiresAt:)` fails with `'Session' initializer is inaccessible due to 'internal' protection level`. The fix is to mark the properties `public` and write a `public init`. Publishing then cascades: no declaration may be more visible than the types in its signature, so a public property of an internal type fails with `property cannot be declared public because its type uses an internal type`. This often escapes unit tests, because `@testable import` exposes the internal initializer. Since Swift 6.4, a private stored property with an initial value no longer drags the memberwise initializer down to `private` when other stored properties are more visible.

code

swift · 8 lines
swift
import Core

func start() {
    let current = currentSession()
    print(current.token)
    let fresh = Session(token: "abc", expiresAt: 0)
    print(fresh)
}

go deeper

for a junior

Recall that a public type's members default to internal and need their own public modifier.

for a middle

Explain why the implicit initializers stay internal and why an explicit public init is required.

for a senior

Diagnose the cascade from the guiding principle, and explain why testable imports let this ship unnoticed until a real client compiles.

for a principal

Set the review rule for a framework's public surface: explicit public inits, types in signatures audited, and a plain-import client in CI.

## Public is opt-in, member by member Swift's book states it as a rule: **a public type defaults to having internal members, not public members**. A framework's API is something you publish deliberately, member by member, so a `public struct Session` with unmarked properties gives other modules a type they can name, store and pass around, and nothing they can do with it. From the Feature module: - reading `session.token` fails with `'token' is inaccessible due to 'internal' protection level`; - calling `Session(token: "abc", expiresAt: 0)` fails with `'Session' initializer is inaccessible due to 'internal' protection level`. Feature can still receive a `Session` from a public Core function and hand it back, but it cannot look inside. ## Implicit initializers never become public Both initializers Swift synthesizes are capped: - the **default initializer** `init()`, synthesized when every stored property has a default value, has the type's level, except that for a `public` type it is `internal`; - the **memberwise initializer** of a struct is `internal` at most, and lower when a stored property's access level pulls it down. So the fix always includes writing the initializer yourself: ```swift public struct Session { public let token: String public let expiresAt: Double var refreshCount = 0 public init(token: String, expiresAt: Double) { self.token = token self.expiresAt = expiresAt } } ``` When Swift synthesizes that initializer at all is covered with initializers; here only its access level matters. ## The guiding principle cascades Swift's overall rule is that **no entity can be defined in terms of another entity with a lower access level**. Once `Session` is public, everything in its public signatures must be at least public: | You make public | It requires | |---|---| | A stored property | its type is public | | A function or initializer | every parameter type and the return type are public | | A generic declaration | its constraint protocols are public | | An enum | its raw-value and associated-value types are public | | A public type's conformance to a public protocol | the requirement implementations are public | The compiler names the problem precisely: `property cannot be declared public because its type uses an internal type`, or `function cannot be declared public because its result uses an internal type`. Nested types inside a public type also default to `internal` and need their own `public`. ## Why the tests did not catch it Core's unit tests use `@testable import Core`, which imports internal declarations as if they were public. The internal memberwise initializer is callable there, so every test constructs `Session` happily, and the failure appears only when the first real client module compiles. Keep at least one test, or a sample client, on a plain `import Core`. ## The Swift 6.4 memberwise change Before Swift 6.4, adding `private var refreshCount = 0` to a struct made its whole memberwise initializer `private`. SE-0502, shipped in Swift 6.4, excludes from the memberwise initializer any property that has an initial value and is less accessible than the most accessible memberwise property, capped at `internal`. The initializer keeps its level and simply drops the parameter; a compatibility overload with the old parameter list survives at its old `private` or `fileprivate` level, so existing calls inside the type keep compiling while other files cannot use it. A `private` stored property **without** an initial value still forces the initializer to `private`. ## Enums, protocols and nested types Not every member defaults the same way, which is worth saying in an interview: - **enum cases** always share their enum's level, so a `public enum` exposes all of its cases with no extra modifier; - **protocol requirements** share the protocol's level, and a `public` type conforming to a `public` protocol must implement each requirement as `public`, because the conformance's level is the lower of the type's and the protocol's; - **nested types** inside a `public` type default to `internal`, like any other member. ## A checklist for a public type 1. Mark every member clients need as `public`, and leave the rest `internal` or narrower. 2. Write an explicit `public init`, even when the memberwise one looks right. 3. Make every type that appears in a public signature public as well, or keep it out of the signature. 4. Compile a plain-import client, or a black-box test, against the framework.

  • Why does a public Swift struct's implicit memberwise initializer never become public, even when every stored property is public?
    The book defines the memberwise initializer as internal at most: private or fileprivate if a stored property pulls it down, otherwise internal. A public initializer is part of a framework's API and its parameter list would change whenever a stored property is added, so Swift makes you write it explicitly.
  • In Swift 6.4, does `struct Draft { var title: String; private var edits = 0 }` still have a memberwise initializer other files of the module can call?
    Yes. Under SE-0502, `edits` has an initial value and is less accessible than `title`, so it is dropped from the memberwise initializer, which stays internal as `Draft(title:)`. Before 6.4 the private property made the whole initializer private. Without the `= 0` the initializer would still be private.

  • Declaring a struct public makes its properties and initializer public too
  • A public struct's implicit memberwise initializer is public as well
  • A public property may use an internal type if the type is in the same module
  • If the @testable unit tests compile, other modules can use the API
  • Any private stored property always makes the memberwise init private in Swift 6.4

Assumes Swift 6.4: SE-0502 stopped a private stored property with an initial value from forcing the implicit memberwise initializer to private when other properties are more visible; earlier compilers did.