<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="">This seems like reasonable feature for me. Combined with throwing subscripts (proposed in another thread), we could translate that into bounds checking as well:<div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class="">let array = [1, 2, 3]</div><div class=""><br class=""></div><div class="">let fifth = array[5] // traps</div><div class=""><br class=""></div><div class="">let fifth = try? array[5] // nil</div></blockquote><div class=""><br class=""></div><div class="">However, I'm still trying to find a use case for catching such errors. Imagine you're making a GUI app (e.g. for iOS), and then write the following code:</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class="">do {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let fifth = try array[5]</div><div class="">} catch BoundsError {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// ???</div><div class="">}<br class=""></div></blockquote><div class=""><br class=""></div>or<div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class="">let sum: Int = Int.max // assuming it has the max value by accident</div></blockquote><blockquote type="cite" class=""><br class=""></blockquote><blockquote type="cite" class=""><div class="">do {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>sum += 1</div><div class="">} catch ArithmeticError {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// ???</div><div class="">}<br class=""></div></blockquote><div class=""><div class=""><br class=""></div>What would you do in the catch clause?<br class=""><div class=""><div class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font color="#929292" class=""><br class="Apple-interchange-newline">Pozdrawiam – Regards,</font></div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font color="#929292" class="">Adrian Kashivskyy</font></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Wiadomość napisana przez Jonathan Hull via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; w dniu 07.12.2015, o godz. 10:29:</div><br class="Apple-interchange-newline"><div class=""><div class="">For context, I just read this thread (that happened before I joined) which discusses the idea of having a variant of arithmetic which can throw on overflow:<br class=""><a href="https://lists.swift.org/pipermail/swift-evolution/2015-December/000292.html" class="">https://lists.swift.org/pipermail/swift-evolution/2015-December/000292.html</a><br class=""><br class=""><br class="">I wonder if having a variant of ‘throws’ might allow this... &nbsp;‘throws!’ would act exactly like throws, but it could be called without ‘try’ (resulting in a crash &nbsp;if it tries to throw). &nbsp;Basically, any statement calling a function/operation with throws! would have the equivalent of an implicit ‘try!' unless there is an actual ‘try’.<br class=""><br class="">It would allow the following (assuming + is marked ‘throws!’):<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let x = 2 + 3 &nbsp;// No crash, No exception<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let y = try Int.max + 1 // Exception, but no crash<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let z = Int.max + 1 // Crash!<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let w = try? Int.max + 1 // nil<br class=""><br class="">Similarly, the thread mentions the desire for a throwing forced unwrap operator, and I think this allows that as well. Assuming the force unwrap operator is marked 'throws!’:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let x:Int? = nil<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let y = try x! // Exception, but no crash<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let z = x! // Crash!<br class=""><br class=""><br class="">Anyway, this is all of the top of my head, so I am sure there is some issue I am missing. &nbsp;I am not entirely sure that this would allow more good than evil overall, but I do like that the ! in ‘throws!’ signifies danger. &nbsp;In the cases above, it is being used to take something which already crashes now, and allow the programmer to catch an exception instead of that crash if they think to look for it.<br class=""><br class="">Thanks,<br class="">Jon<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></div></body></html>