I have the protocol that conform Equatable
protocol TestProtocol: Equatable {
var id: Int {get}
}
func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
return lhs.id == rhs.id
}
To have opportunities to store TestProtocol value we should use type erasure.
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
var id: Int {
return item.id
}
private let item: T
init(_ testProtocol: T) {
self.item = testProtocol
}
}
And, after all, we can use it, like this struct TestStruct: TestProtocol { let id: Int }
let a = TestStruct(id: 1)
let b = TestStruct(id: 1)
a == b /// true
// let a = TestStruct(id: 1)
// let b = TestStruct(id: 0)
// a == b /// false
And all ok. But I want to use TestProtocol
with required init method, such as init(id: Int)
. How can I implement AnyTestProtocol
If TestProtocol
contains required init method?
protocol TestProtocol: Equatable {
var id: Int {get}
/// Required init method for every protocol implementation
init(id: Int)
}
func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
return lhs.id == rhs.id
}
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
var id: Int {
return item.id
}
private let item: T
required init(id: Int) {
????????
}
init(_ testProtocol: T) {
self.item = testProtocol
}
}
Just forward the required init(id:)
call to T
:
class AnyTestProtocol<T: TestProtocol>: TestProtocol {
// ...
required init(id: Int) {
self.item = T(id: id)
}
// ...
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments