Quick question about protocol composition

I assume the answer is no, but I've not found any definitive answer to this elsewhere so:

Is there any difference (at all, besides syntax) between P and Q here:

protocol P: FixedWidthInteger & UnsignedInteger {
    // ...
}

protocol Q: FixedWidthInteger, UnsignedInteger {
    // ...
}

or between foo and bar here:

func foo<T>(_ v: T) -> UInt8
    where T: FixedWidthInteger & UnsignedInteger
{
    return UInt8(v & 0xff)
}

func bar<T>(_ v: T) -> UInt8
    where T: FixedWidthInteger, T: UnsignedInteger
{
    return UInt8(v & 0xff)
}

?

1 Like

You're right. Protocol composition was originally introduced to avoid creating additional wrappers for expressing types that conform to several protocols.

When you use protocol compositions as types, they represent a type that conforms to its components. As conformances and constraints, they simply represent a list of protocols.

However, it would be reasonable to ask @Douglas_Gregor about that "at all".

1 Like

There's no difference; the compiler will treat them the same way.

Doug

2 Likes