[swift-evolution] Immutable Structures
Brent Royal-Gordon
brent at architechies.com
Sat Dec 26 19:33:18 CST 2015
> > "In a regular method, `self` is a regular (constant) parameter, but in a `mutating` method, `self` is `inout` so that it can be mutated and those mutations will reach the caller's copy of the instance. There's no need for an `inout` `self` on reference types, because the mutations are performed on the shared instance which both the caller and callee have a reference to, so `mutating` only really makes sense for value types."
>
> But the `mutating` keyword isn't really needed to make `self` work this way for structures. It's actually not strictly needed at all. Another thing the `mutating` keyword "allows" is for the method to be able to reassign properties. I was advocating that `mutating` had only that meaning in order to be more consistent. Then it would be equally useful for classes to make APIs more clear with respect to mutation.
I don't think you quite understand what I'm saying. You can mutate properties in a `mutating` method *because* `self` is an `inout` parameter in a mutating method. That's how mutating the properties is implemented.
--
Brent Royal-Gordon
Architechies
More information about the swift-evolution
mailing list