Pitching Optional Throws in Swift

Great comment. 100% agree.

-1 on the proposal from me.

I don’t think this proposal is a good idea. It complicates the language considerably while hiding dangerous behavior, which would require code auditing to really understand.

fatalError(_:) should be used extremely sparingly, as it hides dangerous code from the call site. If you really need that behavior, try! is usually more appropriate. Of course, the real issue is that any throwing function can throw anything conforming to Error, limited only by documented conventions.

I personally believe the only way to improve on error handling is to introduce typed throws. That wouldn’t even be a breaking change: throwing functions could implicitly throw Error, while non-throwing functions throw Never.

By making errors part of the type signature, you might even be able to permit overloading the normal way. You could also deprecate a lot of existing keywords, such as rethrows, or simply reduce them to semantic sugar.

1 Like
Terms of Service

Privacy Policy

Cookie Policy