Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gfx/swift-swifteither
A PoC of an ideal error handling in Swift
https://github.com/gfx/swift-swifteither
Last synced: 26 days ago
JSON representation
A PoC of an ideal error handling in Swift
- Host: GitHub
- URL: https://github.com/gfx/swift-swifteither
- Owner: gfx
- Created: 2015-01-01T00:16:16.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-13T00:34:30.000Z (over 9 years ago)
- Last Synced: 2023-04-09T21:19:26.119Z (over 1 year ago)
- Language: Swift
- Size: 170 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SwiftEither [![Build Status](https://travis-ci.org/gfx/Swift-SwiftEither.svg?branch=master)](https://travis-ci.org/gfx/Swift-SwiftEither)
An Either pattern implementation in Swift
# SYNOPSIS
```swift
import SwiftEither// helpers
struct Error {
var reason: String
init(_ reason: String) { self.reason = reason }
}func success(s: String) -> Either {
return Either(success: s)
}
func failure(s: String) -> Either {
return Either(failure: Error(s))
}// 1. basic use
let x = success("foo")
// x represents either success:String or failure:Error
switch x {
case .Success(let s):
println(s.value) // "foo"
case .Failure(let f):
println(f.value.reason) // not reached
}// 2. Either chaining, just like to optional chaining
success("success").chain({ (m) -> Either in
return success("chained")
}) // "chained"failure("error!").chain({ (m) -> Either in
return Either(success: "chained")
}) // "error!"// 3. Fallback operation, just like to null coalescing operator
success("success").fallback({
return success("chained")
}) // "success"failure("error!").fallback({
return success("fallback")
}) // "fallback"// fallback operator
failure("error!") ?? success("fallback") // Either(success:"fallback")
failure("error!") ?? "fallback" // Either(success:"fallback")
```# DESCRIPTION
This is a proof-of-concept of an ideal error handling in Swift.
# AUTHOR
Fuji, Goro (gfx) [email protected]
# LICENSE
The Apache 2.0 License