条件协议一致性?
Conditional protocol conformance?
我想做这样的事情:
class SomeClass<Element> { }
extension SomeClass: SomeProtocol where Element: String { }
它告诉我:
Extension of type "SomeClass" with constraints cannot have an inheritance clause.
到目前为止,我本可以发誓这是 protocol/extension/generic/associatedtype 范式的基本特征之一。还有其他方法可以实现吗?
作为 Paul,您现在可以在 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
}
这是在 Swift 4.1 中实现的。
我想做这样的事情:
class SomeClass<Element> { }
extension SomeClass: SomeProtocol where Element: String { }
它告诉我:
Extension of type "SomeClass" with constraints cannot have an inheritance clause.
到目前为止,我本可以发誓这是 protocol/extension/generic/associatedtype 范式的基本特征之一。还有其他方法可以实现吗?
作为 Paul,您现在可以在 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
}
这是在 Swift 4.1 中实现的。