<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="">"some resource acquired"</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="">"some resource released"</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="">"Entered scope"</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="">"Going out of scope"</span><span class="">)<br></span> }<br><span class=""> </span><span class="">print</span><span class="">(</span><span class="">"Left scope"</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 'let raii = ' line: "Initialization of immutable value 'raii' was never used; consider replacing with assignment to '_' or removing it"<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>