Created
April 12, 2022 15:22
-
-
Save noppefoxwolf/0b9551a394ec26d6987821bd0620a32b to your computer and use it in GitHub Desktop.
Case: using type erasure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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