I know that if a (struct
's) stored property is declared as "let
," then the property can't have mutating
operations done on it. Does that cascade to the containing object? In other words, will whole-object assignment work, or does a instance-property-level let
make the whole object immutable?
Whole-object assignment still works.
Struct properties are (I think) the only place in the language where let
isn't usually the right default. The main reason to use let
instead of var
in a struct is if you want to enforce that multiple properties have to be kept in sync, and so it's better to create a whole new value instead. (You can also enforce this with private(set)
, but then your enforcement doesn't extend to extensions in the same file.)
6 Likes