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?

8 Likes

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.

8 Likes

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.

3 Likes

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

7 Likes

I also just came up with a similar thread.

2 Likes

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

4 Likes

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.

1 Like
Terms of Service

Privacy Policy

Cookie Policy