Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+ is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics

Tags:

swift

swift4

I'm trying to learn Swift and am looking at an old Generic example that worked in Swift 2

func increment<T: Strideable>(number: T) -> T {
    return number + 1
}

Now in Swift 4 it complains

'+' is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics

Why am I getting this error and what I am doing wrong?

like image 212
Crystal Avatar asked Oct 19 '17 21:10

Crystal


1 Answers

Rather than using the + operator, you can simply use Strideable.advanced(by:).

func increment<T: Strideable>(number: T) -> T {
    return number.advanced(by: 1)
}

increment(number: 2) //returns 3
increment(number: 2.5) //returns 3.5
like image 184
Dávid Pásztor Avatar answered Nov 17 '22 21:11

Dávid Pásztor