I'm trying to write a static generic method which takes a protocol as an argument and register class instance in Swinject container as a protocol resolve. It's important that I could not register a module as a protocol it does not conforms to.
I wrote something like this:
/// RegisterableModule guarantee that conformer has `create()` method returning self
public extension RegisterableModule {
static func registerModule<P>(asProtocol proto: P.Type,
in container: Container) {
container.register(proto, name: nil) { (resolver) -> P in
return self.create()
}
}
}
It does not compile because obviously Self may be not conforming to P
I also tried to specify generic constraint using where
:
where Self: P
does compile error "Type 'Self' constrained to non-protocol, non-class type 'P'"where self: P
does multiple compiles error.where Self: P.Type
does compile error "Type 'Self' constrained to non-protocol, non-class type 'P.Type'"where self: P.Type
does multiple compile errors.
I also wonder if I can specify a constraint that P can be protocol only.