[swift-evolution] Getting a list of protocol conformers

Greg Parker gparker at apple.com
Thu Nov 17 17:11:20 CST 2016


> On Nov 15, 2016, at 7:53 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I would like to be able to get, at runtime, an array of all types conforming to a particular protocol.  (Similarly, I would like to be able to get an array of all subtypes of a given type).  Is this in the generics manifesto?  If not, can it be added?  What is the timeframe?
> 
> It seems to me, that the compiler should actually already have this information, and it is just a matter of keeping it around when it is requested.  I could be wrong about that though...

The compiler does not have sufficient information to do this. Dynamic libraries and resilience can add new protocol-conforming types and new protocol-conformaning extensions to existing types.

This sort of search could be implemented by the runtime, but it would likely be slow and memory-hungry. The Swift runtime wants to be as lazy as possible about type metadata, in order to reduce launch time and memory footprint. Any operation that requires inspecting every type in every library will defeat this laziness.


> Also, if there is a way to do this now (even if it is slow), I would appreciate the help…

The existing protocol conformance metadata might be sufficient to do this, but I don't think there is any runtime affordance to query it this way.


-- 
Greg Parker     gparker at apple.com     Runtime Wrangler




More information about the swift-evolution mailing list