Undocumented protocol requirement feature?

Hello everyone,
I think I have found (by chance) an undocumented Swift feature today. It seems Protocol can require conforming types to be of a certain class type, like in this example:

class A { }
protocol P: A { }
class B: A, P { }  // Ok
class C: P { }  // Error: 'P' requires that 'C' inherit from 'A'

The official documentation here only mentions "inherited protocols". It might be worth adding a small paragraph to talk about this cool but little known Swift feature.


Hey tiam, that seems like it could be a reasonable thing to add. The best way to get these sorts of things tracked is to send them in via Feedback Assistant.

Hi Kyle,
Ok, thank you for your suggestion!
I will send them a message using the Feedback Assistant today.

This is somewhat implicitly documented. From the linked page:

You can also aggregate the conformance requirements of multiple protocols using protocol composition types, as described in Protocol Composition Type and Protocol Composition

If you look at the page for Protocol Composition Type

Each item in a protocol composition list is one of the following; the list can contain at most one class

Using those two pieces of information, one can piece together that having 1 class is ok, but yeah, it is not super obvious.

After you send something in Feedback Assistant, can you post the issue number here? I'm the writer currently maintaining the documentation, so I'll pull the bug into my queue.

1 Like

Hi Alex,
The issue number is FB7528837.
Thanks everyone!

1 Like