Failable.swift 445 B

1234567891011121314151617
  1. enum Failable<T> {
  2. case success(T)
  3. case failure(Error)
  4. var value: T? { return iif(success: id, failure: const(nil)) }
  5. var error: Error? { return iif(success: const(nil), failure: id) }
  6. func iif<U>(success: (T) -> U, failure: (Error) -> U) -> U {
  7. switch self {
  8. case .success(let value):
  9. return success(value)
  10. case .failure(let error):
  11. return failure(error)
  12. }
  13. }
  14. }