<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This wouldn't change anything with value types. Even if you're not allowed to say:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">array[0].name = "Sam"</div><div class=""><br class=""></div></blockquote>You can still say:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">array[0] = Person(name: "Sam")</div><div class=""><br class=""></div></blockquote>which is equivalent; you're just making it less convenient and harder to optimize. Piecewise mutation of value types is a feature, not a bug, and doesn't have most of the pitfalls of shared mutable reference types.<div class=""><br class=""></div><div class="">-Joe<br class=""><div class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 30, 2016, at 7:27 AM, Maximilian Hünenberger 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=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class="">Hi all,<div class=""></div><div class=""><br class=""></div><div class="">If you have an Array which is declared as mutable variable all contents are implicitly mutable. This is unfortunate especially in case of <u class="">value types</u>.</div><div class=""><br class=""></div><div class="">Consider this code example:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; struct Person { var name: String }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; var array = [Person(name: "Smith")]</div><div class="">&nbsp; &nbsp; // all persons are implicitly mutable</div><div class="">&nbsp; &nbsp; array[0].name = "Sam"</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">So I propose a language feature which addresses this issue:</div><div class=""><br class=""></div><div class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; var array: [let Person] = [Person(name: "Smith")]</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; // all persons are immutable</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; array[0].name = "Sam" // error</span></div></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; // but still allowing to add and remove persons</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; array[0] =&nbsp;Person(name: "Sam")</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><br class=""></div><div class="">For clarification: The semantics are the same as if you've wrapped the struct in a class with a "let" property:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; class ConstantWrapper&lt;T&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; let value: T</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; init(_ value: T) { self.value = value }</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; var array = [ConstantWrapper(Person(name: "Smith"))]</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; // all persons are "indirect" immutable</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; array[0].value.name = "Sam" // error</span></div></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">This model would allow for more immutability in mutable contexts which ultimately leads to less bugs.</span></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">##Possible Extensions:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">We could also allow a "var" declaration:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp; let array: [var Person] = ...</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="">The array models a fixed length array which is highly suggested by some people but you cannot assign a new "Person" to a specific index which is unfortunate. Although this could be solved by tweaking the current model.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Best regards</div><div class="">- Maximilian</div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></div></body></html>