In a Swift protocol, what separates a `{ get }` property requirement from a `{ get set }` one, and which conformer properties satisfy each?
answer
- requirements name a property, not its storage
- settable is a minimum, never a maximum
- constants and read-only computed properties
- access through the protocol type
basics
~20 sA { get } requirement accepts any property of that name and type: a let, a var or a read-only computed property. A { get set } requirement needs something settable: a stored var or a computed property with a setter.
solid answer
~50 sA protocol property requirement names a property and its type, and says only whether it must be gettable or gettable *and* settable; it never says stored or computed. `var capacity: Int { get }` is satisfied by a `let`, a `var` or a read-only computed property, and a conformer may expose more than the protocol asks. `var hitCount: Int { get set }` rules out a `let` and a get-only computed property; the compiler rejects them with the note "candidate is not settable, but protocol requires it". The requirement is always written with `var`, even when a conformer uses a constant, and `static` marks a type property requirement. Through the protocol type, only what the protocol promised is available: assigning a `{ get }` property via `any Cache` is a compile error even when the concrete type declares a `var`.
code
swift · 32 linesprotocol Cache {
var capacity: Int { get }
var hitCount: Int { get set }
func value(forKey key: String) -> String?
mutating func store(_ value: String, forKey key: String)
}
struct InMemoryCache: Cache {
let capacity: Int
var hitCount = 0
private var storage: [String: String] = [:]
init(capacity: Int) { self.capacity = capacity }
func value(forKey key: String) -> String? { storage[key] }
mutating func store(_ value: String, forKey key: String) { storage[key] = value }
}
final class DiskCache: Cache {
var capacity: Int { 10_000 }
private var hits = 0
var hitCount: Int {
get { hits }
set { hits = newValue }
}
func value(forKey key: String) -> String? { nil }
func store(_ value: String, forKey key: String) {}
}
var cache: any Cache = InMemoryCache(capacity: 3)
cache.hitCount += 1
print(cache.capacity, cache.hitCount)
var mem = InMemoryCache(capacity: 2)
print(mem.capacity)go deeper
Recall the table: { get } takes a let, a var or a read-only computed property; { get set } needs a settable var or a computed property with a setter. Requirements are always written with var.
Explain that requirements describe access, not storage, and show the compiler's not-settable note. Mention that the protocol type exposes only the getter even when the concrete type has a setter.
Show judgment in contract design: ask for { get } unless callers really assign through the abstraction, and know the setter-access rule that rejects private(set) against an internal { get set } requirement.
Frame property requirements as API surface: every { get set } is a promise every conformer must keep and every caller may use. Prefer narrow read requirements plus explicit mutating methods when invariants must be protected.
## What a property requirement promises A Swift **protocol** is a list of requirements that conforming types must satisfy. A **property requirement** names an instance or type property and its type, and says one thing about access: whether the property must be **gettable** (`{ get }`) or **gettable and settable** (`{ get set }`). It does not say whether the conformer stores the value or computes it. That freedom is the point: an in-memory cache and an on-disk cache can expose the same `capacity` while one stores a number and the other computes it. ```swift protocol Cache { var capacity: Int { get } var hitCount: Int { get set } } ``` Rules for writing the requirement itself: - It is always declared with `var`. Writing `let capacity: Int` inside a protocol is rejected: "protocols cannot require properties to be immutable; declare read-only properties by using 'var' with a '{ get }' specifier". - It always carries an accessor block, `{ get }` or `{ get set }`. - A **type property** requirement is prefixed with `static`; a class may satisfy it with a `static` or a `class` property. ## What satisfies each kind | Conformer declares | `{ get }` | `{ get set }` | |---|---|---| | `let` stored constant | yes | no | | `var` stored property | yes | yes | | read-only computed property | yes | no | | computed property with `get` and `set` | yes | yes | The asymmetry is the whole rule: `{ get }` is a **minimum**, so a conformer may add a setter for its own code; `{ get set }` is a requirement for a setter, so a constant or a get-only computed property cannot meet it. When a conformer falls short, the compiler reports "type 'BadCache' does not conform to protocol 'Cache'" with the note "candidate is not settable, but protocol requires it" pointing at the offending declaration. Two further details catch people out: 1. **Access level of the setter.** If the protocol is `internal` and requires `{ get set }`, a conformer's `private(set) var hitCount` fails: "setter for property 'hitCount' must be declared internal because it matches a requirement in internal protocol 'Cache'". The setter must be as visible as the requirement. 2. **The protocol type hides extra setters.** Code that holds the value as `any Cache` (or as a generic `C: Cache`) sees only what the protocol promised. With `var c: any Cache = InMemoryCache(capacity: 3)`, the line `c.capacity = 5` is rejected as a get-only property even though `InMemoryCache` declared `var capacity`. On the concrete type, the same assignment compiles. ## Mutability through the protocol type A `{ get set }` requirement makes the property settable through the protocol, but the usual value rules still apply. For a protocol that any type can adopt, a `let` constant of type `any Cache` cannot have `hitCount` assigned, because the boxed value might be a struct. A `var` existential can. (Protocols restricted to classes behave differently, because the box then always holds a reference; that is a separate rule about class-only protocols.) ## Why the design matters - **Freedom of implementation.** A disk-backed conformer can compute `capacity` from a configuration file while an in-memory conformer stores it; callers never know. - **Honest contracts.** Asking for `{ get set }` when callers only read forces every conformer to expose a setter it may not want. Start with `{ get }` and widen only when callers genuinely assign. - **Diagnostics point at the candidate.** When conformance fails, read the notes: they name the requirement and the candidate that missed it, which is usually faster than reading the whole type. In an interview, the strongest answer states the table above in one breath, then adds the two traps: the setter's access level, and the fact that the protocol type exposes only what the protocol declares.
- Can a conformer satisfy `var hitCount: Int { get set }` with `private(set) var hitCount = 0`?Not when the protocol is visible at that level: for an `internal` protocol the compiler says the setter "must be declared internal because it matches a requirement in internal protocol". The setter must be at least as accessible as the requirement it satisfies. If outside code should not assign, the requirement should have been `{ get }`.
- A struct declares `var capacity: Int` for a `{ get }` requirement. Why does `c.capacity = 5` fail when `c` is `any Cache`?Through the protocol type, only the protocol's promises are visible, and it promised a getter. The struct's extra setter exists only on the concrete type. Assign through a variable of type `InMemoryCache`, or widen the requirement to `{ get set }` if callers really need to set it through the abstraction.
- A { get } requirement forces the conformer to use a computed property.
- A let constant can satisfy a { get set } requirement if the type is a class.
- Protocols declare read-only property requirements with let.
- A conformer with an extra setter can be assigned through the protocol type.
- private(set) is enough to satisfy a { get set } requirement.