类型应为通用函数采用什么协议,以在Swift中将任何数字类型作为参数? [英] What protocol should be adopted by a Type for a generic function to take any number type as an argument in Swift?
问题描述
我想让一个函数在Swift中接受任何数字(Int,Float,Double,...)
I want to make a function accept any number (Int, Float, Double, ...) in Swift
func myFunction <T : "What to put here"> (number : T) -> {
//...
}
不使用NSNumber
without using NSNumber
推荐答案
更新:以下答案原则上仍然适用,但是Swift 4完成了对数字协议的重新设计,因此添加自己的东西通常是不必要的。在构建自己的系统之前,请先阅读标准库的数字协议。
Update: The answer below still applies in principle, but Swift 4 completed a redesign of the numeric protocols, such that adding your own is often unnecessary. Take a look at the standard library's numeric protocols before you build your own system.
在Swift中,这实际上是不可能的。为此,您需要创建一个新协议,并使用通用函数中要使用的任何方法和运算符进行声明。该过程将为您工作,但是确切的细节将在一定程度上取决于您的通用功能。这是对要获得数字 n
并返回(n-1)^ 2
的函数的处理方式
This actually isn't possible out of the box in Swift. To do this you'll need to create a new protocol, declared with whatever methods and operators you're going to use inside your generic function. This process will work for you, but the exact details will depend a little on what your generic function does. Here's how you'd do it for a function that gets a number n
and returns (n - 1)^2
.
首先,定义您的协议,包括运算符和带有 Int
的初始化程序(这样我们就可以
First, define your protocol, with the operators and an initializer that takes an Int
(that's so we can subtract one).
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
和所有整数类型执行此操作:
All of the numeric types already implement these, but at this point the compiler doesn't know that they conform to the new NumericType
protocol. You have to make this explicit -- Apple calls this "declaring protocol adoption with an extension." We'll do this for Double
, Float
, and all the integer types:
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
协议作为通用约束来编写实际函数。
Now we can write our actual function, using the NumericType
protocol as a generic constraint.
func minusOneSquared<T : NumericType> (number : T) -> T {
let minusOne = number - T(1)
return minusOne * minusOne
}
minusOneSquared(5) // 16
minusOneSquared(2.3) // 1.69
minusOneSquared(2 as UInt64) // 1
这篇关于类型应为通用函数采用什么协议,以在Swift中将任何数字类型作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!