我有一个相关类型的协议. 我希望只有当关联的类型符合Equatable时,协议才会强制使用某些方法.
大概是这样的:
public protocol ListOfThings {
associatedtype ThingType
func append(_ thing: ThingType)
// This function makes sense only if ThingType is equatable
func remove(_ thing: ThingType) where ThingType: Equatable
}
我只是找不到与之对应的正确语法.
我也try 了一个扩展,但编译器(正确地)声明了函数体.
extension ListOfThings where ThingType: Equatable {
func remove(_ thing: ThingType)
}