假设我有这些协议:
protocol SomeProtocol {
}
protocol SomeOtherProtocol {
}
现在,如果我想要一个采用泛型类型的函数,但该类型必须符合SomeProtocol
,我可以:
func someFunc<T: SomeProtocol>(arg: T) {
// do stuff
}
但是有没有办法为多个协议添加类型约束?
func bothFunc<T: SomeProtocol | SomeOtherProtocol>(arg: T) {
}
在类似的情况下,它会使用逗号.这是我试过的.
<T: SomeProtocol | SomeOtherProtocol>
<T: SomeProtocol , SomeOtherProtocol>
<T: SomeProtocol : SomeOtherProtocol>