[swift-users] Constraining the conforming type of a protocol

Joanna Carter joanna at carterconsulting.org.uk
Sun Jul 30 03:25:59 CDT 2017


IMHO, there is a very subtle difference between :

protocol Toggling : Equatable
{
  …
}

extension Toggling
{
  …
}

… and :

protocol Toggling
{
  …
}

extension Toggling where Self : Equatable
{
  …
}

The first says that any type that implements Toggling also implements Equatable.

The second says that an implementing type can be Toggling without necessarily having to be Equatable.

Either way around, you are providing a default implementation for toggled() and toggle(), but, with the first version, toggled requires that Self is also Equatable.

Therefore, you are saying that, because the default implementation of Toggling demands that Self is Equatable, you have to also state that everything that is Toggling is also Equatable.

Since :

protocol Toggling where Self : Equatable

… is functionally identical to :

protocol Toggling : Equatable

… why would you want to use a more verbose way of saying the same thing ?

I would venture to suggest that there is no need for the longer syntax and that your bug report is not necessary

:-)

Joanna

--
Joanna Carter
Carter Consulting



More information about the swift-users mailing list