0

I am trying to take advantage of generics like so.

protocol Y { ... }
protocol X: Y { ... }

protocol A: Collection where Element == Y { ... }
protocol B: A where Element == X { ... }

I am getting the error on the definition of B:

Same-type constraint type 'X' does not conform to required protocol 'Y'

But, X does conform to Y...

I tried using an intermediary associatedtype DataPoint like so

protocol A: Collection where Element == DataPoint { 
    associatedtype DataPoint: Y
    ... 
}
protocol B: A where DataPoint == X { ... }

But I get the same error. Is it possible to accomplish this sort of thing in Swift 4 or do I have to make two completely separate protocols?

Hamish
  • 78,605
  • 19
  • 187
  • 280
jjatie
  • 5,152
  • 5
  • 34
  • 56
  • 1
    I get a better error message, "Associated type 'Self.Element' cannot be equal to both 'Y' and 'X'" – which makes perfect sense. Surely you meant `Element : Y` & `Element : X`? – Hamish Jun 21 '17 at 21:20
  • Although "But, `X` does conform to `Y`" – no it doesn't, [protocols don't conform to themselves](https://stackoverflow.com/a/43408193/2976878). – Hamish Jun 21 '17 at 21:23
  • @Hamish I just understood your first comment as I thought that's what I was doing. Yes, I did mean that, thank you very much. – jjatie Jun 21 '17 at 21:43
  • New syntax available! `protocol B: A where Element: Y&X` – GetSwifty Jun 21 '17 at 22:08

0 Answers0