直接对泛型类型进行类型约束与使用 'where' 子句之间是否存在实际差异?
Is there a practical difference between a type constraint on a generic type directly vs using a 'where' clause?
这两个函数在功能上有什么实际区别吗?
func testX<T>(value:T) where T:StringProtocol {
// Do something
}
func testY<T:StringProtocol>(value:T){
// Do something
}
它们似乎都可以编译并且 运行 很好。想知道为什么会有两种不同但看似相同的语法。
没有区别。第一种形式
func testX<T>(value: T) where T: StringProtocol
与 SE-0081 Move where clause to end of declaration 一起引入以提高可读性,特别是对于较长的约束列表。基本原理是从通用参数列表中删除 where
子句,例如
func foo<S: Sequence where S.Element == Int>(seq: S)
成为
func foo<S: Sequence>(seq: S) where S.Element == Int
in Swift 3.作为side-effect,即使是简单的约束,如
您的 T: StringProtocol
可以移动到新引入的 where-clause.
这两个函数在功能上有什么实际区别吗?
func testX<T>(value:T) where T:StringProtocol {
// Do something
}
func testY<T:StringProtocol>(value:T){
// Do something
}
它们似乎都可以编译并且 运行 很好。想知道为什么会有两种不同但看似相同的语法。
没有区别。第一种形式
func testX<T>(value: T) where T: StringProtocol
与 SE-0081 Move where clause to end of declaration 一起引入以提高可读性,特别是对于较长的约束列表。基本原理是从通用参数列表中删除 where
子句,例如
func foo<S: Sequence where S.Element == Int>(seq: S)
成为
func foo<S: Sequence>(seq: S) where S.Element == Int
in Swift 3.作为side-effect,即使是简单的约束,如
您的 T: StringProtocol
可以移动到新引入的 where-clause.