SwiftResult

0.2.0

A `Result` type for Swift compatible with SE-0235
koher/SwiftResult

What's New

Apply modifications of SE-0235

2018-12-10T03:11:49Z
  • Rename Value to Success
  • Rename Error to Failure
  • Rename value to success
  • Rename error to failure
  • Rename unwrapped to get

SwiftResult

Build Status

SwiftResult provides a Result type which is compatible with the Result type proposed in SE-0235 (announcement about modifications), which may be added to the Swift standard library in Swift 5.x. Replacing third-party Result types with it may make it easier to migrate your code to Swift 5.x.

// An overload to return a `Result` instead of `throws`
extension JSONDecoder {
    func decode<T: Decodable>(_ type: T.Type, from json: JSON) -> Result<T, DecodingError> {
        ...
    }
}

let json: JSON = ...

let person: Result<Person, DecodingError> = JSONDecoder().decode(Person.self, from: json)

switch person {
case .success(let person):
    ... // Success
case .failure(let error):
    ... // Failure
}

let age: Result<Int, DecodingError> = person.map { $0.age }
do {
    let age: Int = try age.get()
    // Uses `age` here
} catch let error {
    // Error handling
}

Installation

Swift Package Manager

Add the following to dependencies in your Package.swift.

.package(
    url: "https://github.com/koher/SwiftResult.git",
    from: "0.2.0"
)

Carthage

github "koher/SwiftResult" ~> 0.2.0

License

Apache License. It follows Swift's license and Swift Evolution's license.

Description

  • Swift Tools 4.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Aug 30 2022 16:25:41 GMT-0500 (GMT-05:00)