I've been thinking about this a lot over the last few days.
There's some flexible syntax here, with I think some history. The where clauses did not completely replace the colon-inheritance syntax, and they can do some things that the colon syntax can't do. And the commas are from before protocol composition with &.
These are, if I have it right:
- comma-separated types in a where clause
- explicit protocol composition with
& in a where clause.
- protocol inheritance (maybe not the right word) like Q : P, along with a where clause
- protocol inheritance with a protocol composition.
- protocol inheritance with comma-separated protocols.
I'm using the word 'protocol inheritance' for Q : P there because the swift book uses that, but the word 'inheritance' makes me nervous.
My intuition is poorly-formed, I realize. I feel like the right way to do this would be either:
protocol Q : P & Equatable
or
protocol Q where Self: P & Equatable
in that order, preferring explicit protocol composition with & and protocol inheritance (because the syntax is simpler.)
3 and 5 are hybrids with some things in the inheritance and some in the where clause. You can express super type relationships in the inheritance position after the colon but not some other relationships like Self.Element: Hashable.
I'm not sure (and did not look up) if there's a difference between protocol composition with & and whatever happens with a comma between the protocols. Opinion: the comma should go away someday and the & should always be used.
In the first example with Self: P, Self: Equatable, the comma separates two different refinements (that are probably equivalent to the others). In some other situation where you had protocol Q where Self: P, Self.Element : Hashable the comma makes sense though. Opinion: it would be nice to find one syntax for all of this.
It is completely possible that I am missing some subtlety here. I did not put this all into a file and start running tests on it, and I did not close re-read the whole section in the Swift book or look at the compiler source today.