[swift-evolution] Throws? and throws!
Jonathan Hull
jhull at gbis.com
Sat Jan 14 20:03:12 CST 2017
My intended framing of this does not seem to be coming across in my arguments. I am not thinking of this as a way to avoid typing ‘try!’ or ‘try?’. This is not intended to replace any of the current uses of ‘throws’. Rather, it is intended to replace trapping and nil-returning functions where converting it to throw would be burdensome in the most common use cases, but still desirable in less common use cases. In my mind, it is only enabling the author to provide extra information and flexibility, compared to the current behavior.
For example, let’s say I have a failable initializer, which could fail for 2 or 3 different reasons, and that the vast majority of use-cases I only care whether it succeeded or not (which is why nil-returning was chosen)… but there may be a rare case or two where I really would prefer to probe deeper (and changing it to a throwing initializer would inhibit the majority cases). Then using ’throws?’ allows the primary usage to remain unchanged, while allowing users to opt-in to throwing behavior when desired.
Right now I end up making multiple functions, which are identical except for throw vs nil-return, and must now be kept in sync. I’ll admit it isn’t terribly common, but it has come up enough that I think it would still be useful.
The other argument I will make is one of symmetry. We have 3 different types of error handling in swift: throwing, optional-returns, and trapping. There is already some ability to convert between these:
If you have a throwing function:
‘try?’ allows you to convert to optional-return
‘try!’ allows you to convert to trapping
If you have an optional-return:
‘!’ allows you to convert to trapping
you are unable to convert to throwing (because it requires extra info which isn’t available)
If you have a trapping function, you are unable to convert to either.
With ‘throws?’ you have an optional return which you can convert to throwing with ‘try’
With ‘throws!’ you have a trapping function where:
‘try?’ allows you to convert to optional-return
‘try’ allows you to convert to throwing
Thus, ‘throws?’ and ‘throws!’ allow you provide optional-return and trapping functions where extra information is provided so that it is possible to convert to throwing when desired. In cases where this conversion is not appropriate, the author would simply continue to use the current methods.
Basically it is useful in designs where optional-return or trapping were ultimately chosen, but there was also a strong case to be made for making it a throwing function. I think the fears of people using it instead of ‘throws’ are unfounded because they already have the ability to use optionals or trapping… this just mitigates some of the losses from those choices.
Does that make more sense?
Thanks,
Jon
> On Jan 12, 2017, at 5:34 PM, Greg Parker <gparker at apple.com> wrote:
>
>
>> On Jan 12, 2017, at 4:46 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>>> On Thu, Jan 12, 2017 at 6:27 PM, Jonathan Hull <jhull at gbis.com <mailto:jhull at gbis.com>> wrote:
>>>
>>> Also, ‘try’ is still required to explicitly mark a potential error propagation point, which is what it was designed to do. You don’t have ‘try’ with the variants because it is by default no longer a propagation point (unless you make it one explicitly with ’try’).
>>
>> If this is quite safe and more convenient, why then shouldn't it be the behavior for `throws`? (That is, why not just allow people to call throwing functions without `try` and crash if the error isn't caught? It'd be a purely additive proposal that's backwards compatible for all currently compiling code.)
>
> Swift prefers that potential runtime crash points be visible in the code. You can ignore a thrown error and crash instead, but the code will say `try!`. You can force-unwrap an Optional and crash if it is nil, but the code will say `!`.
>
> Allowing `try` to be omitted would obscure those crash points from humans reading the code. It would no longer be possible to read call sites and be able to distinguish which ones might crash due to an uncaught error.
>
> (There are exceptions to this rule. Ordinary arithmetic and array access are checked at runtime, and the default syntax is one that may crash.)
>
>
> --
> Greg Parker gparker at apple.com <mailto:gparker at apple.com> Runtime Wrangler
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170114/e9613a76/attachment.html>
More information about the swift-evolution
mailing list