[swift-evolution] typed throws

Brent Royal-Gordon brent at architechies.com
Mon Aug 21 01:05:05 CDT 2017

> On Aug 17, 2017, at 11:27 PM, John McCall via swift-evolution <swift-evolution at swift.org> wrote:
> The only practical merit of typed throws I have ever seen someone demonstrate is that it would let them use contextual lookup in a throw or catch.

Let me take this in a slightly different direction.

I largely agree with you that most code shouldn't limit the types of errors it can throw. But I still want still want typed throws because I want to address a completely different problem: generics. Other than the useful but limited abilities of `rethrows`, there is currently no way to sensibly abstract over the error behavior of a piece of code. In particular, there's no way for a single protocol to support both throwing and non-throwing conformances.

One very straightforward and sensible way to handle this would be to support typed `throws` and make `Never` a subtype of `Error`; then you could handle error behavior abstraction using generic parameters, associated types, `where` clauses, and all those nice things we've already invented and taught people how to use. The other alternative is to invent several generics features from whole cloth purely to handle `throws`.

If we want to support errors in generics, designing typed `throws` and its interactions with the generics system seems a lot easier than designing a novel mechanism. Perhaps there are complications I'm not aware of—the thought of figuring out how generics are implemented fills me with dread—but naïvely, I would guess that the typed-throws-based solution will be easier to implement, too. And of course typed `throws` also helps the people who think they want to, you know, specify the type that a function throws. And it might help with the systems programming challenges we've talked about in this thread. So we can solve several different problems with a single feature, even if we think some of those problems are kind of niche.

I don't think any of the individual reasons to support typed `throws` are home runs, but I think this thread has produced some pretty solid ground balls and a couple of well-timed bunts, and that might be enough to get it across home plate.

Brent Royal-Gordon

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

More information about the swift-evolution mailing list