<div dir="ltr">At the risk of appearing glib or naive - which isn&#39;t my intention, I&#39;d like to know the answer - is there not a similar argument to be made for any function which returns an optional instead of throwing a more descriptive error? Asking an array for its first element returns an optional because of the possibility it might have no elements in it; should this throw an error instead of being &#39;failable&#39;?</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 2, 2016 at 11:35 PM, Haravikk via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><span class=""><div>On 2 Mar 2016, at 23:07, Chris Lattner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br></span><div><span class="">On Mar 2, 2016, at 1:11 PM, James Campbell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><blockquote type="cite"><br>Given that we now have error handling and availability checks does it make sense to have Failable Initializers which date back to Swift 1.1?<br></blockquote><br></span><span class="">Our error handling schema (<a href="https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#kinds-of-error" target="_blank">https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#kinds-of-error</a>) defines how error conditions are handled, and one important class of them (e.g. the &quot;string to int&quot; case) is best modeled as returning an optional.  This works really well in practice for functions/methods in general. </span></div></blockquote><br></div><div>Could you give an example of why failable is the better fit here? To me the following two statements are identical:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>let a = FailableType()</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>let b = try? ThrowableType()</font></div><div><br></div><div>Except that in the latter case the try? is more explicit about what is happening (and that it can fail), and I have the option of catching the error to find out more about what went wrong. With some optimisation it should be possible for try? to be just as efficient as a failable initialiser I think.</div><div><br></div><div>That said, the failable initialiser could have the same explicit call syntax if it required a trailing question-mark, e.g:</div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>let a = FailableType()?</font></div><div><br></div><div>As currently the only indicator is on the initialiser declaration itself. Still, when it comes to debugging I’ve found it very useful to force myself to use error handling instead, as it means I have to give reasons for why something failed, which can make it easier to track issues when they do arise.</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>