Why are structs in Swift said to be immutable?

If you call that function on global itself, you will have overlapping write-read accesses, since:

So self is considered as being modified for the entire body of the function. Within the function, you can read/write any instance properties on self; they are not considered overlapping, because you're already within a non-instantaneous access.

However, if you access the same variable but don't use self (e.g. you access it via global), that will begin a new access. That access conflicts with the access you're already in. It doesn't matter if you read/write from global; as you're already within a modify/write access to the same variable, all other accesses are considered conflicts.

two accesses to the same variable are not allowed to overlap unless both accesses are reads

(Note: the law of exclusivity was later relaxed to allow overlapping accesses if both are atomic. It doesn't matter for this question, I'm just mentioning it for completeness)

1 Like