In my project, there is an OC function that searches for Class that match a specific Protocol *object, now I want to write a swift version to limit the generic parameter is a protocol and return the same type, not AnyObject.
But I found that I can't, Swift will always return theProtocol.Protocol, here is the example:
public func classConforming<P>(to protocolName: P.Type) -> P.Type? {
// find the class
}
let theClass = classConforming(to: theProtocol.self)
// theClass: theProtocol.Protocol?
And I can't use the static functions in theProtocol, it only works when I return Any and cast it to theProtocol.Type
I also tried
public func classConforming<P: Protocol>(to protocolName: P) -> P.Type? {
// find the class
}
func classConforming<T>(to protocolName: T.Protocol) -> some T.Type?
where T: protocol { ... }
Note the spelling T: protocol, which I intended to invoke a similar idea to C# where T: class, where T: struct. I did not say T: Protocol because, again, that means the Objective-C Protocol class.
I've wanted the ability to say this for other reasons too, like
func saveAsTypes<P1, P2, C>(first: P1.Protocol, second: P2.Protocol, object: C)
where C: P1 & P2
which isn't an unreasonable constraint on the face of it, but Swift has no spelling for.