<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 11, 2016, at 2:38 AM, Dan Raviv via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Would it make sense to allow mutable properties in structs, which could be mutated even by non-mutating methods?<div class=""><br class=""></div><div class="">I've noticed this could be useful when implementing a non-mutating protocol method. The method semantically doesn't change the state of the protocol's implementor, and therefore shouldn't be declared as mutating. However, *some* implementations might need to change some internal state for implementing the functionality. Making specific struct properties mutable for such a case could make sense.</div><div class=""><br class=""></div><div class="">Alternatively, Swift could allow implementing a non-mutating protocol method as a mutating method in the implementor. Currently, this doesn't seem to be &nbsp;allowed; Swift doesn't recognize the mutating method as a match for the non-mutating protocol method.</div></div></div></blockquote><br class=""></div><div>We currently have the property that, even in generic code, we can assume that an immutable value of type T is safe from read-read races, either because all types are never changed by immutable operations or are accessed through runtime calls with the appropriate synchronization (retain/release, weak references). To preserve these 'let' semantics with mutable fields, it would have to be the implementor's responsibility to ensure its mutating accesses are properly synchronized. Note that you can already get the effect of a mutable field in a struct by embedding a reference to a mutable class reference:</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div>struct Foo {</div><div>&nbsp; var x: Int</div><div>&nbsp; class MutableFields {</div><div>&nbsp; &nbsp; var y: Int</div><div>&nbsp; &nbsp; init(y: Int) { self.y = y }</div><div>&nbsp; }</div><div><br class=""></div><div>&nbsp; private var mutableFields: MutableFields</div><div>&nbsp; var y: Int {</div><div>&nbsp; &nbsp; get { return mutableFields.y }</div><div>&nbsp; &nbsp; set { mutableFields.y = newValue }</div><div>&nbsp; }</div><div>}</div></blockquote><div class=""><br class=""></div><div class="">This can for instance be used to implement a memoized lazy field, as in <a href="https://github.com/robrix/Memo" class="">https://github.com/robrix/Memo</a>.</div><br class=""><div class="">-Joe</div></body></html>