Determining what protocols a type conforms to

Is there any way to get a complete list of what protocols a type conforms to?

In this case, I'm exploring the standard library to get a better understanding of the various protocols involved.

I know I can click through declarations in Xcode, which I've been doing so far, but there's no guarantee I haven't missed a protocol added in an extension somewhere.

1 Like

You should be able to find this in the API documentation. For example (for Array):

https://developer.apple.com/documentation/swift/array#relationships

Thank you for that pointer. Of course, that assumes the documentation is guaranteed to be correct. If that is true because the documentation is automatically generated to be correct for a given version of the Swift standard library, what mechanism is used to generate that list of protocols?

1 Like

This sort of information used to be easily found on swiftdoc.org, which was created by Nate Cook (@nnnnnnnn). However, that website has not been updated since Swift 3.1.

Personally, I think it would be fantastic if the official Swift project were to host a similar webpage with autogenerated documentation. The visual protocol hierarchies are especially useful, as is being able to see all the members of a type on one page.

Furthermore, the official Apple-hosted documentation does not specify the constraints on extension methods. For example, MutableCollection.sort is only available where Self: RandomAccessCollection, but the documentation never mentions that requirement.

8 Likes

I'd also like to know how to access / generate such info.

Yes, that part of the documentation is generated automatically, so it should always be current.

Unfortunately, I don't know how the tools behind developer.apple.com query that information.