我不确定where
子句可以将通用参数限制为从某个协议继承的协议。
protocol Edible {}
protocol PetFood: Edible {}
struct CatFood: PetFood {}
struct Rocks {}
func eat<T: Edible>(_ item: T) -> String {
return "Just ate some \(type(of: item))"
}
let food: CatFood = CatFood()
eat(food) //"Just ate some CatFood"
let moreFood: PetFood = CatFood()
//eat(moreFood) //Cannot invoke 'eat' with an argument list of type '(PetFood)'
func eatAnything<T>(_ item: T) -> String {
return "Just ate some \(type(of: item))"
}
eatAnything(moreFood) //This works, obviously
eatAnything(Rocks()) //But, of course, so does this...
有没有办法限制eatAnything()
允许协议类型,但只限制那些从Edible
继承的协议类型?
答案 0 :(得分:1)
在您的示例中,泛型函数的定义没有任何意义,因为它可以替换为:
EntrySet
但是如果你真的想使用泛型函数那么你应该知道:
通用功能的定义
func eat(_ item: Edible) -> String {
return "Just ate some \(type(of: item))"
}
func eat<T: Edible>(_ item: T) -> String { ... }
func eat<T>(_ item: T) -> String where T: Edible { ... }
协议是动态类型,因此它们使用后期绑定。通用代码在编译期间转换为正常,需要早期绑定
可以使用与协议兼容的类型定义通用函数,但是此函数不能将此协议作为类型传递,因为编译器不知道该类型是什么{{ 1}}。传递给泛型函数类型必须是特定类型(class,struct,enum,...)
func eat<T: Edible>(_ item: T) -> String where T: Equatable { ... }