[swift-evolution] [Swift 4.0] Conditional conformances via protocol extensions

Manav Gabhawala manav1907 at gmail.com
Wed Aug 3 12:17:17 CDT 2016

In the generics manifesto
conditional conformances via protocol extensions is under unlikely.

The manifesto notes:

"This is an extremely powerful feature: is allows one to map the
abstractions of one domain into another domain (e.g., every Matrix is
a Graph). However, similar to private conformances, it puts a major
burden on the dynamic-casting runtime to chase down arbitrarily long
and potentially cyclic chains of conformances, which makes efficient
implementation nearly impossible."

Correct me if I’m completely wrong here but I think this is a super
useful feature and is worth talking about.
Firstly why its super useful if not immediately clear is cause you can
do neat things like:

extension Collection : Equatable where Element: Equatable

And you can even make protocols in a module that you did not write
conform to an arbitrary other protocol and give it a default

I was wondering why this would put any more of a burden on the runtime
than simple inheritance of protocols. The way this could be
implemented is to augment the ConformanceTable for nominal types by
looking up its protocol extension’s inheritance clauses. I can
definitely see this impacting compile time but I don’t see why runtime
performance will be any different than simple inheritance. Further,
cyclic chains can be detected and broken (compiler error) during the
second pass of semantic analysis.

I had a (perhaps naïve) implementation here:
https://github.com/apple/swift/pull/2233 and I think now would be a
great time to talk about such a feature.

Its possible I completely overlooked something but I would like a more
a detailed explanation about why this isn’t possible.

Manav Gabhawala

More information about the swift-evolution mailing list