[swift-evolution] Get a list of Concrete Implementers for Protocols
Joe Groff
jgroff at apple.com
Tue Oct 3 11:54:36 CDT 2017
> On Oct 3, 2017, at 7:08 AM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>
> Hi Evolution,
>
> We talked a while back about being able to get a list of all concrete implementations for a protocol (i.e. all the types that adhere to it). Is this still being planned?
>
> There are a couple of big use-cases for this:
>
> • The first is for plug-ins and other things that require registration. A protocol can provide a way for them to be auto-registered by calling a static method from the protocol on each of them.
>
> • The second is that you can build robust + extensible factories.
>
>
> I definitely have use for this right now. I am currently writing a UI generator that auto-generates functioning UI based on a given set of criteria/data. Right now, I have to manually register each partial-template (of which there will eventually be several thousand) with the factory by hand, which is an error-prone process. It is one of those things that was doable in ObjC pretty easily, but I still haven’t figured out how to do it in Swift.
It's not on our roadmap, but it's definitely in our mind as we plan for ABI stability. The runtime metadata for a Swift binary is already structured so that protocol conformances can be looked up and enumerated at runtime. There's a special section in Mach-O binaries, and an exported symbol in ELF/COFF binaries, that includes a list of all conformances defined in the image. Even though the current runtime does not provide API for this functionality, it could be retrofitted using the existing conformance table in Swift binaries. (When the ABI stabilizes, a third-party library could even conceivably provide this functionality.)
-Joe
More information about the swift-evolution
mailing list