What does “Protocol … can only be used as a generic constraint because it has Self or associated type requirements” mean?

Protocol Observing inherits from protocol Hashable, which in turn inherits from protocol Equatable. Protocol Equatable has the following requirement: func ==(lhs: Self, rhs: Self) -> Bool And a protocol that contains Self somewhere inside it cannot be used anywhere except in a type constraint. Here is a similar question.

In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?

In Swift 4 it is now possible to declare a variable that is a subclass of a type and implements one or more protocols at the same time. var myVariable: MyClass & MyProtocol & MySecondProtocol To do an optional variable: var myVariable: (MyClass & MyProtocol & MySecondProtocol)? or as the parameter of a method: func … Read more

Unable to use protocol as associatedtype in another protocol in Swift

The problem, which David has already alluded to, is that once you constrain a protocol’s associatedtype to a specific (non @objc) protocol, you have to use a concrete type to satisfy that requirement. This is because protocols don’t conform to themselves – therefore meaning that you cannot use Address to satisfy the protocol’s associated type … Read more

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

Suppose for the moment we adjust your protocol to add a routine that uses the associated type: public protocol RequestType: class { associatedtype Model var path: String { get set } func frobulateModel(aModel: Model) } And Swift were to let you create an array of RequestType the way you want to. I could pass an … Read more

Why can’t a get-only property requirement in a protocol be satisfied by a property which conforms?

There’s no real reason why this shouldn’t be possible, a read-only property requirement can be covariant, as returning a ConformsToB instance from a property typed as ProtocolB is perfectly legal. Swift just currently doesn’t support it. In order to do so, the compiler would have to generate a thunk between the protocol witness table and … Read more

Protocol doesn’t conform to itself?

Why don’t protocols conform to themselves? Allowing protocols to conform to themselves in the general case is unsound. The problem lies with static protocol requirements. These include: static methods and properties Initialisers Associated types (although these currently prevent the use of a protocol as an actual type) We can access these requirements on a generic … Read more