[swift-users] Every non-trivial Swift function should throw, right?

John Spurlock john.spurlock at gmail.com
Tue Mar 8 11:55:31 CST 2016


The error handling rationale document [1] is an excellent review, thanks.

If you are going to consider optional returns as appropriate for a
certain class of errors, that class needs to be crystal clear - and
enforceable/hinted-at by the compiler.  Java and C# exception
hierarchies have rationale, but end-users don't read documentation.
Since they can only be minimally enforced by the type system, it has
turned out to be a free-for-all.

Too bad Result<T> does not exist!  The Result handling flow looks
depressingly familiar (as mentioned above), it's just as tedious and
error-prone to do by hand as C/Go-style error checking, so it's weird
that the current error system does not have first-class support for
them under the same rationale.

Perhaps Optional<T> and Result<T> would be best thought of as
fundamentally related in some way, and have a similar protocol/shape
called Either<T,Or> where Or is nil for optionals and
ErrorType/NSError for results.  Or consider optionals an error of a
certain kind for simplicity.

[1] https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst

>  (And again with more detail in the original "rationale" doc.)
>
> Jordan


More information about the swift-users mailing list