<div dir="ltr"><div>Currently, when a reference-type adopts a protocol with a function declared as `mutating`, the reference-type&#39;s implementation cannot call that function internally. This is because the compiler enforces an immutable `self` pointer value, and the `mutating` qualifier implies that the function implementation may mutate that `self` pointer value.</div><div><br></div><div>However, there seems to be a number of fairly reasonable situations where a reference-type implementation of these `mutating` functions may only want to mutate properties owned by `self`, but not the actual `self` pointer value.</div><div><br></div><div>Consider this toy example:</div><div><br></div><div>```</div><div>import Foundation</div><div><br></div><div>protocol RandomDataTransformable {</div><div>    typealias TransformableType</div><div>    var data: [TransformableType] { get set }</div><div>    </div><div>    mutating func addRandomData()</div><div>}</div><div><br></div><div>extension RandomDataTransformable where TransformableType == Int {</div><div>    mutating func addRandomData() {</div><div>        let random = Int(arc4random_uniform(6) + 1)</div><div>        data.append(random)</div><div>    }</div><div>}</div><div><br></div><div><br></div><div>/////</div><div><br></div><div>// VALID</div><div>struct NumberSource_Struct : RandomDataTransformable {</div><div>    typealias TransformableType = Int</div><div>    var data: [Int] = []</div><div>    </div><div>    mutating func addData() {</div><div>        addRandomData()</div><div>    }</div><div>}</div><div><br></div><div><br></div><div>// VALID</div><div>class NumberSource_ClassDeclaration: NSObject, RandomDataTransformable {</div><div>    typealias TransformableType = Int</div><div>    var data: [Int] = []</div><div>}</div><div><br></div><div>var numberSource = NumberSource_ClassDeclaration()</div><div>numberSource.addRandomData()</div><div><br></div><div><br></div><div>// INVALID</div><div>class NumberSource_ClassImplementation: NSObject, RandomDataTransformable {</div><div>    typealias TransformableType = Int</div><div>    var data: [Int] = []</div><div>    </div><div>    func addData() {</div><div>        self.addRandomData() // Compiler Error: Cannot use mutating member on immutable value: &#39;self&#39; is immutable</div><div>    }</div><div>}</div><div>```</div><div><br></div><div>Even despite the fact that the default implementation for `addRandomData` does not mutate the `self` pointer value, reference-type implementations are unable to call that function internally, since it is marked as `mutating`.</div><div><br></div><div>Perhaps confusingly, `addRandomData` may be called by externally, by objects which own instances of the reference-type (even though, again, it may not called internally by the implementation, itself).</div><div><br></div><div>Currently, the only solution to allow reference-type implementations to call the sample `addRandomData` implementation internally is to qualify the whole `RandomDataTransformable` protocol as `class`. The downside here is that this takes an otherwise perfectly reference- and struct-compatible protocol + extension implementation and restricts it to only apply to classes, decreasing overall code reusability.</div><div><br></div><div>My proposal would be to introduce an intermediate mutation qualifier that applies when protocols are adopted by reference-types. The qualifier would specify that the `self` pointer value itself may not be mutated, but `self`&#39;s properties may be, as appropriate.</div><div><br></div><div>Thoughts, feedback on this?</div></div>