<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I don’t think&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">UnsafeMutablePointer</span><span style="font-family: Menlo; font-size: 11px;" class="">.</span><span style="font-family: Menlo; font-size: 11px; color: rgb(61, 29, 129);" class="">alloc</span><span style="font-family: Menlo; font-size: 11px;" class="">(capacity)</span>&nbsp;qualifies as “pure”, so you’d have to handle it before failing:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>?(capacity: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.pointer =&nbsp;<span style="color: rgb(112, 61, 170);" class="">UnsafeMutablePointer</span>.<span style="color: rgb(61, 29, 129);" class="">alloc</span>(capacity)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> capacity &gt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">100</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">pointer</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">dealloc</span>(capacity)</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// Ok, because everything that has side effects has been dealt with</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.capacity = capacity</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div class="">or, as in Kostiantyn’s example, bail out before anything with side effects has been done:</div><div class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="color: rgb(187, 44, 162);" class="">init</span>?(capacity: <span style="color: rgb(112, 61, 170);" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> capacity &gt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">100</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// This is ok, we haven’t actually done anything yet</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.pointer =&nbsp;<span style="color: rgb(112, 61, 170);" class="">UnsafeMutablePointer</span>.<span style="color: rgb(61, 29, 129);" class="">alloc</span>(capacity)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.capacity = capacity</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">Either way, in this case we don’t really lose anything because current system doesn’t prevent this kind of memory leak in the first place. This doesn’t bother the playground at all:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span> MyArray&lt;T&gt; {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> pointer: </span>UnsafeMutablePointer<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&lt;</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> capacity: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>?(capacity: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">pointer</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UnsafeMutablePointer</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">alloc</span>(capacity)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">capacity</span> = capacity</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> capacity &gt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">100</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">return</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">nil</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;</span>// Oops! Memory leak!</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;}</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; </span><span style="font-family: Menlo; font-size: 11px;" class="">}</span></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(0, 132, 0);" class="">//&nbsp;</span><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">Will probably leak anyway, since structs don’t have deinits.</span></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div class="">What if we allowed functions to have&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">where</span>&nbsp;clauses?</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">struct</span>&nbsp;MyArray&lt;T&gt; {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp; &nbsp;&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">var</span><span style="color: rgb(0, 0, 0);" class="">&nbsp;pointer:&nbsp;</span>UnsafeMutablePointer<span style="color: rgb(0, 0, 0);" class="">&lt;</span>T<span style="color: rgb(0, 0, 0);" class="">&gt;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">var</span>&nbsp;capacity:&nbsp;<span style="color: rgb(112, 61, 170);" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">init</span>?(capacity:&nbsp;<span style="color: rgb(112, 61, 170);" class="">Int&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">where&nbsp;</span>capacity &lt;=&nbsp;<span style="color: rgb(39, 42, 216);" class="">100</span>) {&nbsp;<span style="color: rgb(0, 132, 0);" class="">// I</span><font color="#008400" face="Menlo" class="">mmediately returns nil if the where clause evaluates to false</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">self</span>.<span style="color: rgb(79, 129, 135);" class="">pointer</span>&nbsp;=&nbsp;<span style="color: rgb(112, 61, 170);" class="">UnsafeMutablePointer</span>.<span style="color: rgb(61, 29, 129);" class="">alloc</span>(capacity)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">self</span>.<span style="color: rgb(79, 129, 135);" class="">capacity</span>&nbsp;= capacity</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(0, 132, 0);" class="">&nbsp; &nbsp;&nbsp;</span>}</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(0, 132, 0);" class="">&nbsp; &nbsp; /* OR (</span><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;" class="">note that the&nbsp;</span><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;" class="">init has become</span><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;" class="">&nbsp;</span><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">non-failable)&nbsp;</span></font><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">*/</span></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">init</span>(capacity:&nbsp;<span style="color: rgb(112, 61, 170);" class="">Int&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">where&nbsp;</span>capacity &lt;=&nbsp;<span style="color: rgb(39, 42, 216);" class="">100</span>)&nbsp;<span style="color: rgb(187, 44, 162);" class="">throws</span>&nbsp;{&nbsp;<span style="color: rgb(0, 132, 0);" class="">//&nbsp;</span><span style="color: rgb(0, 132, 0);" class="">I</span><font color="#008400" face="Menlo" class="">mmediately</font><font color="#008400" face="Menlo" class="">&nbsp;throws an error if the where clause evaluates to false</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">self</span>.<span style="color: rgb(79, 129, 135);" class="">pointer</span>&nbsp;=&nbsp;<span style="color: rgb(112, 61, 170);" class="">UnsafeMutablePointer</span>.<span style="color: rgb(61, 29, 129);" class="">alloc</span>(capacity)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">self</span>.<span style="color: rgb(79, 129, 135);" class="">capacity</span>&nbsp;= capacity</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(0, 132, 0);" class="">&nbsp; &nbsp;&nbsp;</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">I know&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">guard</span>&nbsp;offers similar functionality, but it can appear anywhere in the function body…&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">where</span>&nbsp;<i class="">must</i> to be up-front, and would get checked before the function body has a chance to do anything. Plus, since it’s part of the function’s signature, it’d appear in API headers. I can’t remember which thread it was in, but as I’ve said before… Which is more useful: a note buried somewhere in the API docs about preconditions, or being able to see the actual bit of code that’ll validate your data?</div><div class=""><br class=""><div class="">
- Dave Sweeris

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Jan 27, 2016, at 10:24, Jean-Daniel Dupas &lt;<a href="mailto:mailing@xenonium.com" class="">mailing@xenonium.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">The discussed sample is explicitly about a UnsafeMutablePointer.alloc(capacity) call that must be explicitly freed. That why I said local variable wouldn’t improve much the issue.</div><div class=""><br class=""></div>You can’t restrict the operations that can be done before an early return. How would you write a failable init that return nil when a ressource allocation or access fail (which is far more common than having a @pure function failing) ?</div></div></blockquote></div><br class=""></div></body></html>