[swift-evolution] Proposal: Allow Type Annotations on Throws

Ricardo Parada rparada at mac.com
Fri Dec 18 20:28:41 CST 2015


Thanks for the clarification.  Why is the compiler saying that the catch is not exhaustive when it is covering all the possible values of the enum?  Is it to be able to catch future values added to the enum type?


> On Dec 18, 2015, at 8:05 PM, David Owens II <david at owensd.io> wrote:
> 
> 
>> On Dec 18, 2015, at 4:38 PM, Ricardo Parada <rparada at mac.com <mailto:rparada at mac.com>> wrote:
>> 
>> Hi David
>> 
>> I started reading your proposal and I have a couple of questions. 
>> 
>> In the Enum Base ErrorType example you mentioned that it requires a "catch { }" clause.  However the code is already covering the two possible Enum values (OffBy1 and MutatedValue). Why is the "catch { }" required? I typed that code into a playground and I did not get any errors. Are you saying that because the Enum type could add a value in the future?
> 
> Playgrounds are basically in an anonymous function that throws, so the problem doesn’t show up there at the top level. Copy this into your playground.
> 
> enum MyError: ErrorType {
>     case OnlyOne
> }
> 
> func thrower() throws { throw MyError.OnlyOne }
> 
> func nolies() {
>     do {
>         try thrower()
>     }
>     catch MyError.OnlyOne { print("handled") }
>     // catch { print("compiler error until uncommented") }
> }
> 
>> Also, you proposed the catch clause to use error as the name of the constant holding the error.  Wouldn't it be better to let the programmer decide the name rather than hard coding it to use error? For example:
>> 
>> catch e where e.value == 0 { print("0") }
>> catch e where e.value == 1 { print("1") }
>> catch { print("nothing") }
> 
> The “error” name is already specified in the Swift rules for what the constant is. I don’t see any compelling reason to propose a change to that.
> 
> -David

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/ab2d308d/attachment.html>


More information about the swift-evolution mailing list