Skip to content

Instantly share code, notes, and snippets.

@noppefoxwolf
Created April 12, 2022 15:22
Show Gist options
  • Save noppefoxwolf/0b9551a394ec26d6987821bd0620a32b to your computer and use it in GitHub Desktop.
Save noppefoxwolf/0b9551a394ec26d6987821bd0620a32b to your computer and use it in GitHub Desktop.
Case: using type erasure
protocol FooRepository {
func foo() -> String
}
final class FooClient {
static let shared = FooClient()
private init() {}
}
extension FooClient: FooRepository {
func foo() -> String {
"Foo"
}
}
final class FooInteractor {
private let fooRepository: AnyFooRepository
init(fooRepository: any FooRepository) {
self.fooRepository = AnyFooRepository(fooRepository)
}
init(fooRepository: FooClient = .shared) {
self.fooRepository = AnyFooRepository(fooRepository)
}
}
class AnyFooRepository: FooRepository {
let _foo: () -> String
func foo() -> String {
_foo()
}
init(_ base: FooRepository) {
_foo = { base.foo() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment