<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> struct Person { var name: String }</div><div><br></div><div> var array = [Person(name: "Smith")]</div><div> // all persons are implicitly mutable</div><div> 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);"> var array: [let Person] = [Person(name: "Smith")]</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> // all persons are immutable</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> 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);"> // but still allowing to add and remove persons</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> array[0] = 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> class ConstantWrapper<T> {</div><div> let value: T</div><div> init(_ value: T) { self.value = value }</div><div> }</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);"> var array = [ConstantWrapper(Person(name: "Smith"))]</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> // all persons are "indirect" immutable</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> 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);"> 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>