7
votes

I want to do something like this:

class SomeClass<Element> { }
extension SomeClass: SomeProtocol where Element: String { }

It tells me:

Extension of type "SomeClass" with constraints cannot have an inheritance clause.

I could have sworn up to this point that this was one of the bread and butter features of the protocol/extension/generic/associatedtype paradigm. Is there another way to implement this?

2
No that is not possible at present. That is the reason why Array does not conform to Equatable even if its elements are Equatable. - Martin R
There is an accepted proposal to implement this in the next swift version: github.com/apple/swift-evolution/blob/master/proposals/… - hariseldon78
This is coming in Swift 4.1! swift.org/blog/conditional-conformance - elasticrat

2 Answers

1
votes

This was implemented in Swift 4.1.

0
votes

As Paul, you can now do it in Swift 4

protocol Nameable {
    var name:String {get set}
}
func createdFormattedName<T:Nameable>(_ namedEntity:T) -> String where T:Equatable {
    //Only entities that conform to Nameable which also conform to equatable can call this function
    return "This things name is " + namedEntity.name
}