<div dir="ltr">I was experimenting in Swift with RAII, a technique I find very useful from C++.  In general it works well, but there is something I found unexpected.<div><br></div><div>Consider this class:</div><div><br></div><div>







<p class=""><span class="">class</span><span class=""> RAII {<br></span><span class="">    </span><span class="">init</span><span class="">() {<br></span><span class="">        </span><span class="">print</span><span class="">(</span><span class="">&quot;some resource acquired&quot;</span><span class="">)<br></span>    }<br><span class="">    </span><span class="">deinit</span><span class=""> {<br></span><span class="">        </span><span class="">print</span><span class="">(</span><span class="">&quot;some resource released&quot;</span><span class="">)<br></span>    }<br>}</p><p class="">And this sample code:<br></p><p class=""><span class="">        </span><span class="">let</span><span class=""> b = </span><span class="">true<br></span><span class="">        </span><span class="">if</span><span class=""> (b)<br></span>        {<br><span class="">            </span><span class="">print</span><span class="">(</span><span class="">&quot;Entered scope&quot;</span><span class="">)<br></span><span class="">            </span><span class="">let</span><span class=""> raii = </span><span class="">RAII</span><span class="">()<br></span><span class="">            </span><span class="">print</span><span class="">(</span><span class="">&quot;Going out of scope&quot;</span><span class="">)<br></span>        }<br><span class="">        </span><span class="">print</span><span class="">(</span><span class="">&quot;Left scope&quot;</span><span class="">)</span></p><p class=""><span class="">As expected, I see the output:</span></p></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><p class=""><span class="">Entered scope<br></span>some resource acquired<br>Going out of scope<br>some resource released<br>Left scope</p></div></blockquote><div><p class=""><span class="">However, the compiler gives me a warning on the &#39;let raii = &#39; line:  &quot;Initialization of immutable value &#39;raii&#39; was never used; consider replacing with assignment to &#39;_&#39; or removing it&quot;<br><br>If I change that line to:</span></p></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><p class=""><span class="">let _ = RAII()</span></p></div></blockquote>the warning goes away, but I get the unexpected output:<br><br><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span class="">Entered scope</span></div><div>some resource acquired</div><div>some resource released</div><div>Going out of scope</div><div>Left scope<br></div><div><br></div></blockquote>It appears the object instance is being destroyed immediately after being created, because the compiler correctly sees that nobody needs it.<div><br></div><div>Therefore, I propose that such instances stay referenced until the end of the scope they are declared in. </div><div><br></div><div>(As an aside, I would like to see a deinit on a struct to support this technique as well.)</div><div><br></div><div>Thanks,</div><div>-Jason-</div></div>