<div dir="ltr">In the example below, solve is a non-mutating func within struct type Matrix, that uses an Array<T> to store its values.<div><br></div><div>This function mutates self, even though it's declared as non-mutating.</div><div><br></div><div>My understanding is that the compiler doesn't make a real copy in the acopy = self instruction, and then provides that contents to the mx_gels_ function which modifies the memory contents.</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(187,44,162)">public</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color:rgb(187,44,162)">func</span><span style="font-variant-ligatures: no-common-ligatures;"> solve(rhs b: </span><span style="color:rgb(79,129,135)">Matrix</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">>) -> </span><span style="color:rgb(79,129,135)">Matrix</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">>? {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        // ...</span></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        </span></span><span style="color:rgb(187,44,162)">var</span><span style="font-variant-ligatures: no-common-ligatures;"> acopy = self</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        // ...<br></span></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre"><br></span></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        </span></span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color:rgb(49,89,93)">mx_gels_</span><span style="font-variant-ligatures: no-common-ligatures;">(&trans, &m, &n, &nrhs, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">>(acopy.</span><span style="color:rgb(79,129,135)">values</span><span style="font-variant-ligatures: no-common-ligatures;">), &lda, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">>(x.</span><span style="color:rgb(79,129,135)">values</span><span style="font-variant-ligatures: no-common-ligatures;">), &ldb, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;"><</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">>(workspace), &lwork, &status);</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">                </span></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        // ...<br></span></span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><span class="inbox-Apple-tab-span" style="white-space:pre">        </span>}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"><br></span></p><div><br></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures: no-common-ligatures;"></span></p><div>Is this expected? I mean, I can force a real copy of course, but value semantics would suggest the code above is correct and wouldn't need that. Shouldn't the cast trigger the copy somehow? Or is there a better way of expressing this operation? Thx.</div></div><div><br></div><div>Raphael</div></div>