Skip to content

Instantly share code, notes, and snippets.

@yimajo
Created February 17, 2020 05:02
Show Gist options
  • Save yimajo/fbca63163b6e5c9dac9e534dbc4723b7 to your computer and use it in GitHub Desktop.
Save yimajo/fbca63163b6e5c9dac9e534dbc4723b7 to your computer and use it in GitHub Desktop.
Swift5.2のcallAsFunctionを使ってUseCaseの必須なメソッドとしてみる
import Foundation
protocol UseCase where Failure: Error {
associatedtype Parameters
associatedtype Success
associatedtype Failure
func callAsFunction(_ parameters: Parameters, completion: ((Result<Success, Failure>) -> ())?)
}
struct User {
let id: UUID
}
class GetUserInteractor: UseCase {
func callAsFunction(_ parameters: UUID, completion: ((Result<User, Never>) -> ())?) {
// 本来ならなんやかんや色々あるけど今回はとりあえず返すだけ
completion?(.success(User(id: parameters)))
}
}
let getUser = GetUserInteractor()
getUser(UUID()) {
switch $0 {
case .success(let user):
print(user)
case .failure:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment