Edit: I've created an updated proposal: https://github.com/apple/swift-evolution/pull/937#pullrequestreview-167116792
And an implementation PR: Add Result<Value, Error> by jshier · Pull Request #19982 · apple/swift · GitHub
Previously on Adding Result:
To summarize: Result
is considered generally useful enough to be included in the standard library. However, in the past, strenuous disagreement between the typed and untyped-throws camps has prevented consensus on the exact form it should take.
To kick off another round of discussion, and before I update my proposal officially, I'd like to discuss the form that was brought up late in the last discussion:
enum Result<Value, Error> {
case success(Value), failure(Error)
}
This spelling prevents many of the issues with both the untyped (lack of error typing) and typed (incompatibility with Swift's current error handling) versions, as well as allowing maximum flexibility for future error handling directions and integration with future async syntax. Yet it also allows integration with the current state of error handling in Swift and can provide the same features as the more constrained spellings. The most common objection to this form is that it isn't opinionated enough, and while I agree slightly in principle, I think the pragmatic gains from including Result
in the language outweigh any philosophical angst I may feel about the failure state not being constrained to Error
in some way.
Barring any startling revelations, and if there's general support, I'll update my proposal PR with this form and associated APIs. This includes support for initialization from a throwing closure, unwrapping into a throwing statement, convenience API for access the value and error, and functional transform functions.