Advice regarding conforming external types to my protocols

I'm seeking some advice regarding conforming types that I do not own to protocols defined in my Swift packages. Specifically, I'm wondering about the best way to go about providing conformances for types which otherwise my package does not require.

Let's say MyPackage vends protocol Foo. Imagine that it is often desirable for OrderedDictionary to conform to Foo. MyPackage does not make use of OrderedDictionary, and clients of MyPackage will of course not always need OrderedDictionary in their code, but in the case that they do need it, it would be desirable for it to conform to Foo. I know that in general it is bad practice to conform types that you do not own to protocols that you do not own, which would imply that MyPackage should supply the conformance of OrderedDictionary to Foo. However, I do not want to force a dependency on swift-collections onto all of the clients of MyPackage.

I'm looking for opinions and advice regarding the best way to handle this type of situation.

1 Like

create a module named _MyPackage_OrderedCollections that contains the third party dependency and the conformance to your protocol, and expose it as a library product.

SwiftPM and certain external tooling (e.g. VSCode) will perceive an additional dependency, but binaries that depend on MyPackage only will not. it is unfortunate that these tools are not intelligent enough to load these package dependencies lazily, but in my opinion that is not a good enough reason to avoid vending the overlay module.

here is an example of this pattern in practice:

3 Likes