Reputation: 1135
I'm trying to figure out how to create a default protocol initializer implementation in a protocol extension that implementing types can inherit.
It looks something like this:
protocol Initializable {
associatedtype EntityType: ConcreteClass
var container: Container<EntityType> { get set }
init()
}
class Container<T: ConcreteClass> {
typealias EntityType = T
let configuration : Configuration
init(config: Configuration) {
configuration = config
}
func getAll() -> [EntityType] {
return [EntityType()]
}
}
extension Initializable {
init(config: Configuration) {
self.init()
self.container = Container<EntityType>(config: config)
}
}
final class Repo: Initializable {
typealias EntityType = String
var container: Container<String>
}
I end up with the following compiler error:
Type 'Repo' does not conform to protocol 'Initializable'. Candidate has non-matching type 'init(type: EntityType.Type)'
Upvotes: 2
Views: 988
Reputation: 337
You use different init in protocol and extension.
init(type: EntityType)
You must implement realization of method init() in extension or in class
Upvotes: 1