38

I want to make a function accept any number (Int, Float, Double, ...) in Swift

func myFunction <T : "What to put here"> (number : T) ->  {
    //...
}

without using NSNumber

mfaani
  • 33,269
  • 19
  • 164
  • 293
ielyamani
  • 17,807
  • 10
  • 55
  • 90

1 Answers1

54

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.


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.

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)
}

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 { }

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
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • 1
    Yay, this is way nicer than having to implement all your own operators like in http://stackoverflow.com/a/28457725/466698 Why isn’t a protocol like this included in Swift itself? – DouglasHeriot Oct 13 '15 at 12:06
  • Some caution. NumericType, as implemented here, as Self requirements. So you wouldn't be able to do something like var values = [NumericType]. If you want to do that, you have to supply your own operators, alas. – user965972 Jun 11 '16 at 21:11
  • 2
    With this method is there a way to convert `T` to an `Int`? Like `Int(number)`? Or use other functions on it like `floor(number)`? – keegan3d Jul 07 '16 at 05:10
  • 1
    In Swift 4 the Numeric protocol is there which defines all but the '/' and '%' operator. This protocol is implemented by both floating point and integer types. – Werner Altewischer Mar 10 '18 at 08:00