Skip to content

Instantly share code, notes, and snippets.

@celian-m
Created November 16, 2018 12:32
Show Gist options
  • Save celian-m/543783556bac33de4a7072bd22458441 to your computer and use it in GitHub Desktop.
Save celian-m/543783556bac33de4a7072bd22458441 to your computer and use it in GitHub Desktop.
import Foundation
enum Either<T> {
case success(T)
case error(Swift.Error)
var successResult: T? {
switch self {
case .success(let result):
return result
case .error(_):
return nil
}
}
}
protocol UseCaseProtocol {
associatedtype ReturnType
associatedtype Param
func execute(_ params: Param, completion: @escaping ((Either<ReturnType>) -> Void))
}
struct UseCase<P, R> {
typealias ReturnType = R
typealias Param = P
init<U>(_ useCase: U) where U: UseCaseProtocol, U.ReturnType == ReturnType, U.Param == Param {
_execute = useCase.execute
}
func execute(_ params: P, completion: @escaping ((Either<R>) -> Void)) {
_execute(params, completion)
}
let _execute: (P, @escaping (Either<R>) -> Void) -> Void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment