<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Hi all,<div></div><div><br></div><div>If you have an Array which is declared as mutable variable all contents are implicitly mutable. This is unfortunate especially in case of <u>value types</u>.</div><div><br></div><div>Consider this code example:</div><div><br></div><div>&nbsp; &nbsp; struct Person { var name: String }</div><div><br></div><div>&nbsp; &nbsp; var array = [Person(name: "Smith")]</div><div>&nbsp; &nbsp; // all persons are implicitly mutable</div><div>&nbsp; &nbsp; array[0].name = "Sam"</div><div><br></div><div><br></div><div>So I propose a language feature which addresses this issue:</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; var array: [let Person] = [Person(name: "Smith")]</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; // all persons are immutable</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; array[0].name = "Sam" // error</span></div></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; // but still allowing to add and remove persons</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; array[0] =&nbsp;Person(name: "Sam")</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><br></div><div>For clarification: The semantics are the same as if you've wrapped the struct in a class with a "let" property:</div><div><br></div><div>&nbsp; &nbsp; class ConstantWrapper&lt;T&gt; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; let value: T</div><div>&nbsp; &nbsp; &nbsp; &nbsp; init(_ value: T) { self.value = value }</div><div>&nbsp; &nbsp; }</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; var array = [ConstantWrapper(Person(name: "Smith"))]</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; // all persons are "indirect" immutable</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; array[0].value.name = "Sam" // error</span></div></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">This model would allow for more immutability in mutable contexts which ultimately leads to less bugs.</span></div><div><br></div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">##Possible Extensions:</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">We could also allow a "var" declaration:</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; let array: [var Person] = ...</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div>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><br></div><div><br></div><div>Best regards</div><div>- Maximilian</div></body></html>