<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 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> var x: Int</div><div> class MutableFields {</div><div> var y: Int</div><div> init(y: Int) { self.y = y }</div><div> }</div><div><br class=""></div><div> private var mutableFields: MutableFields</div><div> var y: Int {</div><div> get { return mutableFields.y }</div><div> set { mutableFields.y = newValue }</div><div> }</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>