<div dir="ltr">In the example below, solve is a non-mutating func within struct type Matrix, that uses an Array&lt;T&gt; to store its values.<div><br></div><div>This function mutates self, even though it&#39;s declared as non-mutating.</div><div><br></div><div>My understanding is that the compiler doesn&#39;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;">&lt;</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">&gt;) -&gt; </span><span style="color:rgb(79,129,135)">Matrix</span><span style="font-variant-ligatures: no-common-ligatures;">&lt;</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">&gt;? {</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;">(&amp;trans, &amp;m, &amp;n, &amp;nrhs, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;">&lt;</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">&gt;(acopy.</span><span style="color:rgb(79,129,135)">values</span><span style="font-variant-ligatures: no-common-ligatures;">), &amp;lda, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;">&lt;</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">&gt;(x.</span><span style="color:rgb(79,129,135)">values</span><span style="font-variant-ligatures: no-common-ligatures;">), &amp;ldb, </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures;">&lt;</span><span style="color:rgb(79,129,135)">T</span><span style="font-variant-ligatures: no-common-ligatures;">&gt;(workspace), &amp;lwork, &amp;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&#39;t need that. Shouldn&#39;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>