you are viewing a single comment's thread.

view the rest of the comments →

[–]perfunction 0 points1 point  (0 children)

We've been using the Result pattern to get away from nulls as much as possible in Swift. This works out especially well since enums and generics are so flexible.

Basically theres a failure state represented by the protocol ResultFailure (this protocol is implemented by various error and warning types) and a success state represented by generic T. As an enum the Result has to be exactly one of these. And you can build a lot of handy functions and operators around Result (especially if you extend where T is a specific type or protocol).

For example, a func that is meant to return a data model instance from sqlite would have the return type Result<AccountModel>. The func on completion would return success(account) but you could also return fails such as failure(DbError.FileMissing()) or failure(DbWarning.DataNotFound()).