Why can't protocol which inherits from PAT but constrains its associated types be used as existential?

I was hoping this code would compile, but it does not:

protocol A {
    associatedtype T

protocol B: A where T == Int {

var demo: B? = nil

I get the usual error: Protocol 'B' can only be used as a generic constraint because it has Self or associated type requirements. Is this error logically necessary in the same way as it is for fully fledged PATs? Or is the code above perfectly well-defined, and the compiler is simply lacking the necessary implementation?


There was a related discussion here a few weeks back. I believe that this is an implementation limitation—this sort of construct would fall out naturally as a consequence of generalized existentials, there just hasn't been enough development effort focused in that area to allow your example to compile.

1 Like

I don't think there are any implementation limitations that would prevent this from being supported now.


Do you think this would fall under the category of bug/fix improvement, or would it need an evolution proposal to move forward?

I think it'd be worth an evolution proposal, just to give us time to discuss possible consequences.


It looks like @anthonylatsis and @Slava_Pestov had worked on a PR to implement this not long ago:


I also just came up with a similar thread.


What a dinosaur! :slight_smile: I am keen to give it another go anytime soon, though.


Is anyone working on a proposal? I am willing to write - or to help write - the proposal. With @anthonylatsis help in the implementation and his experience in this issue, I’m positive we could finally fix this agitating behavior of the generics system.

1 Like

Hi Filip, that sounds great! Unless one is already under way, consider opening a PR against https://github.com/apple/swift-evolution once you have a draft ready so we can launch a pitch and all start discussing, reviewing and refining the proposal together.


Hi guys (@filip-sakel, @anthonylatsis), I just wanted to poke you both with regard to this feature because it came up again in my work just now, and I'll breath such a sigh of relief when this feature is eventually deployed. I myself am totally swamped with coding items on my todo list so I understand entirely that you guys may not have the time, but I just want to express the gratitude that I'll have if you do end up implementing it!

1 Like

Hey Jeremy, the proposal is in progress, we just need some time to wrap things up and launch the review process.

Speaking of which, legitimate examples of real-world applications would be very helpful in improving the motivation!

Terms of Service

Privacy Policy

Cookie Policy