更新: 以下答案原则上仍然适用,但是Swift
4完成了数字协议的重新设计,因此通常不需要添加自己的数字协议。在构建自己的系统之前,请查看标准库的数字协议。
在Swift中,这实际上是不可能的。为此,您需要创建一个新协议,并使用您将在泛型函数中使用的任何方法和运算符进行声明。此过程将为您工作,但是确切的细节在一定程度上取决于您的通用函数的功能。这是获取数字
n并返回的函数的处理方法
(n- 1)^2。
首先,定义您的协议,使用运算符和一个接受一个的初始化程序
Int(这样我们就可以减去一个)。
protocol NumericType { func +(lhs: Self, rhs: Self) -> Self func -(lhs: Self, rhs: Self) -> Self func *(lhs: Self, rhs: Self) -> Self func /(lhs: Self, rhs: Self) -> Self func %(lhs: Self, rhs: Self) -> Self init(_ v: Int)}所有的数值类型都 已经实现了这些 ,但是此时编译器不知道它们符合新
NumericType协议。您必须明确指出这一点-
Apple称其为“宣布扩展的协议采用”。我们将针对
Double,
Float和所有整数类型执行此操作:
extension Double : NumericType { }extension Float : NumericType { }extension Int : NumericType { }extension Int8 : NumericType { }extension Int16 : NumericType { }extension Int32 : NumericType { }extension Int64 : NumericType { }extension UInt : NumericType { }extension UInt8 : NumericType { }extension UInt16 : NumericType { }extension UInt32 : NumericType { }extension UInt64 : NumericType { }现在我们可以使用
NumericType协议作为通用约束来编写实际功能。
func minusOneSquared<T : NumericType> (number : T) -> T { let minusOne = number - T(1) return minusOne * minusOne}minusoneSquared(5) // 16minusoneSquared(2.3) // 1.69minusoneSquared(2 as UInt64) // 1


