<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="">Now that we can return nil from a failable initializer without having initialized all the properties, it’s easier to make a mistake. For example, consider the following (artificial) code:<div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> MyArray&lt;T&gt; {</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT'; 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><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&gt;</span></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" 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; line-height: normal; font-family: 'Akkurat TT'; min-height: 15px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" 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; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; &nbsp; &nbsp; <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; line-height: normal; font-family: 'Akkurat TT';" 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; line-height: normal; font-family: 'Akkurat TT'; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>// Here we should also free the memory. In other words, duplicate the code from deinit.</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" 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><p style="margin: 0px; line-height: normal; font-family: 'Akkurat TT'; min-height: 15px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; line-height: normal; font-family: 'Akkurat TT'; min-height: 15px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">deinit</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT'; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>pointer<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">destroy</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>capacity<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">}</div></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">In the `return nil` case, we should really free the memory allocated by the pointer. Or in other words, we need to duplicate the behavior from the deinit.</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">Before Swift 2.2, this mistake wasn’t possible, because we knew that we could count on deinit being called, *always*. With the current behavior, return `nil` is easier, but it does come at the cost of accidentally introducing bugs. As Joe Groff pointed out, a solution would be to have something like “deferOnError” (or in this case, “deferOnNil”), but that feels a bit heavy-weight to me (and you still have to duplicate code).</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">In any case, I think it’s nice that we can now return nil earlier. I don’t like that it goes at the cost of safety, but I realize it’s probably only making things less safe in a small amount of edge cases.</div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Akkurat TT';" class="">Chris</div></body></html>