Why this fires¶
A field that declares get but no set is read-only after construction.
Struct literals and field defaults still initialize the stored value directly,
but ordinary assignments must go through a setter.
struct Account {
balance: int {
get
}
}
let account = Account { balance: 10 }
account.balance = 20 // error[TE144]Compound assignments such as += and ??= also require a setter.
Fix it¶
Add a setter when mutation is part of the field's API:
balance: int {
get
set(value) {
field = value
}
}Otherwise, remove the assignment and expose the state change through a method that preserves the struct's invariants.