In Swift, what does `let` freeze when the constant holds a struct value, and what does it freeze when it holds a class instance?
answer
- the constant stores what, exactly?
- whole value vs the reference
- var properties behind a let
- parameters behave like let
basics
~10 sA let struct is entirely immutable: no property can be assigned, even one declared var. A let class reference cannot be pointed at another instance, but the instance's var properties can still change.
solid answer
~50 sA constant stores its value. For a struct the value *is* the properties, so `let cart = Cart()` followed by `cart.total = 10` fails with "cannot assign to property: 'cart' is a 'let' constant", and mutating methods cannot be called on it either. For a class the value is a **reference**, so `let` freezes only which instance the constant points to: `cart.total = 10` compiles, while `cart = Cart()` fails with "cannot assign to value: 'cart' is a 'let' constant". Function parameters are constants too, so a function cannot change a struct argument's properties but can change a class argument's. A property declared `let` inside the type is fixed in both cases. The catch: a `let` struct is only as frozen as its properties are values — a class reference stored inside it can still be used to change that object.
code
swift · 16 linesstruct CartValue { var total = 0 }
final class CartObject { var total = 0 }
let valueCart = CartValue()
valueCart.total = 10 // error: cannot assign to property: 'valueCart' is a 'let' constant
let objectCart = CartObject()
objectCart.total = 10 // OK: changes the instance, not the reference
objectCart = CartObject() // error: cannot assign to value: 'objectCart' is a 'let' constant
func applyDiscount(to cart: CartValue) {
cart.total = 0 // error: cannot assign to property: 'cart' is a 'let' constant
}
func applyDiscount(to cart: CartObject) {
cart.total = 0 // OK: the caller's instance changes
}go deeper
Remember the two outcomes: a let struct rejects every property assignment, while a let class reference only rejects pointing the name at another instance.
Explain it from what the constant stores: the whole value for a struct, a reference for a class, and apply the same model to function parameters and let properties.
Point out where the guarantee leaks: a let struct holding a class reference, and class models passed around as let references that are mutated elsewhere.
Use the difference when setting conventions: let struct models give real read-only guarantees to callers, while class models need all-let properties or a single mutating owner.
## What a constant stores `let` in Swift means *this name's value never changes after initialization*. The difference between structs and classes comes entirely from **what the value is**: - For a **struct** (or enum), the value is the instance itself — all of its stored properties, inline. - For a **class**, the value is a **reference** to an instance that lives elsewhere; the instance's properties are not part of the constant's value. Once you hold that model, every rule below follows from it. ## `let` on a struct value Declaring a struct value with `let` freezes the **whole value**: 1. Assigning to any stored property fails, *including properties declared with `var`*. The compiler reports `cannot assign to property: 'valueCart' is a 'let' constant`. 2. Calling a `mutating` method fails, because a mutating method may change any property (the material on methods covers how `mutating` works). 3. Reading is unrestricted, and you can copy the value into a `var` and change the copy. The same applies to a function parameter of struct type: parameters are constants, so `cart.total = 0` inside the function is rejected with `cannot assign to property: 'cart' is a 'let' constant`. The caller's value could not be affected anyway, because the function received its own copy. ## `let` on a class instance Declaring a class instance with `let` freezes only the **reference**: - `objectCart = CartObject()` is rejected: `cannot assign to value: 'objectCart' is a 'let' constant`. - `objectCart.total = 10` compiles and runs, because it changes the instance, not the reference stored in the constant. - A function that takes a class parameter can change the caller's instance through it, even though the parameter itself is a constant. The book makes the same point with two `let` constants referring to one `VideoMode` instance: you can still change the shared `frameRate`, because the constants themselves never change. ## Properties declared `let` Inside a type, a stored property declared with `let` gets its value once — from its default value or during initialization — whichever kind of type holds it. Assigning `cart.id = 2` later fails with `cannot assign to property: 'id' is a 'let' constant`, whether `cart` is a `var` struct or a class instance. To make a class instance effectively immutable you declare **every** stored property `let`; marking the class `final` additionally stops a subclass from adding mutable state. | Declaration | Reassign the name | Assign a `var` property | Assign a `let` property | |---|---|---|---| | `var` struct | yes | yes | no | | `let` struct | no | no | no | | `var` class reference | yes | yes | no | | `let` class reference | no | yes | no | ## Copying out of a constant Because a struct constant holds a value, you can always copy it into a variable and change the copy: - `var editable = valueCart` gives a fully mutable, independent cart; `valueCart` is untouched. - `var other = objectCart` gives a second reference that *can* be pointed at another instance later — but until then both names still reach the same object, and a write through either is visible through both. So `var` versus `let` decides what the **name** may do, while struct versus class decides whether two names can ever reach the same data. ## Where the guarantee stops A `let` struct is frozen only as deeply as its properties are values. If the struct stores a class reference, `let` freezes *which* object it refers to, not the object: `cart.coupon.percentOff = 50` compiles on a `let cart` when `Coupon` is a class with a `var percentOff`. That shared state is its own interview question; the short rule is that **`let` gives deep immutability only for values that are values all the way down**. ## Why it matters in practice - A `let` model struct handed to a view or a function is a real guarantee: nobody can change it underneath you. - A `let` reference to a class model is not: it says nothing about whether the object changes. - In code review, `let` on a class instance is still worth using — it prevents accidentally pointing the name at a different object — but it should never be read as "this object is read-only".
- How do you make a class instance effectively immutable in Swift?Declare every stored property with `let`, so nothing can be reassigned after initialization, and mark the class `final` so no subclass can add mutable state. Then holding it through a `let` or a `var` reference makes no difference to what can change: nothing can. A `let` property that holds another mutable class instance is still a door into mutable state, so the rule applies all the way down.
- Can you assign a `var` property of a struct that is stored in a property of a `let` class instance?It depends on the class's property, not on the `let` reference. If a class declares `let settings: Settings` and `Settings` is a struct, `object.settings.theme = ...` fails because the struct value is part of a constant. If the class declares `var settings`, the same line compiles even when `object` itself is a `let` reference, because `let` on the reference does not freeze the instance's `var` properties.
- A let constant holding a class instance makes the object read-only.
- On a let struct only reassignment is blocked; its var properties stay writable.
- A function can change its struct argument's properties and the caller sees the change.
- A let struct is deeply immutable even when it stores a class reference.
- Marking a class final stops its var properties from being changed.