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

David Owens II david at owensd.io
Fri Dec 18 19:05:24 CST 2015


> On Dec 18, 2015, at 4:38 PM, Ricardo Parada <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/1d85ba22/attachment.html>


More information about the swift-evolution mailing list