"+" deprecated: Mixed type addition is deprecated in Swift 3.1
When I directly add an integer value (ex: 1,2,3 etc.) with another integer variable
let arr:Array = ["One","Two"]
var valueT:Int64 = 0
value = arr.count + 1 //in this line
I am getting the following warning:
'+' is deprecated: Mixed-type addition is deprecated. Please use explicit type conversion.
I set this warning with :
value = Int64(value + 1)
Although fixed , but I want to know why it was called mixed type addition since I did not use ++ . Also is there a better way to fix the warning in swift 3.1?
Update:
The following image is proof of the warning. I am using Xcode Version 8.3 (8E162).
allROR
- array here.
source to share
Edit: To generate an error with your code, it should look like
let value = 5
let result: Int64 = value + 1
Now you will get a warning
'+' deprecated: Mixed type addition is deprecated. Please use explicit type conversion.
But it looks like the warning is misleading as value
and 1
has a type Int
, so its summing as well Int
, so you just need to convert the result to Int64
, and that's what you do, and that's okay.
let result: Int64 = Int64(value + 1)
source to share
Just to answer this part: why its called mixed type addition
With a simplified example of Nirava D:
let value = 5
let result: Int64 = value + 1
You can click the button +
and view the generated interface Collection
: (After indexing has finished, of course.)
@available(swift, deprecated: 3.0, obsoleted: 4.0, message: "Mixed-type addition is deprecated. Please use explicit type conversion.")
public func +<T>(lhs: T.Stride, rhs: T) -> T where T : SignedInteger
So, in the above code example, the type 1
is inferred as Int64
, and as the Int64.Stride == Int
operation value + 1
matches the signature func +<T>(lhs: T.Stride, rhs: T) -> T where T : SignedInteger
.
This deviation is included in the revised version SE-0104 Protocol -Reasonable Integers , this part:
The Standard Library no longer provides the + and - operators for Strideable types.
They were problematic, because you could write mixed code like let x: Int64 = 42; x + = (1 as Int) which will compile, but shouldn't. In addition, since the signed unsigned style is signed, the Standard Library had to implement a hack in code such as let x: UInt = 42; x + = (1 as Int) is ambiguous. These operators were only needed because they made collection indexes convenient, which is longer since the introduction of the new indexing model in Swift 3.
As you've seen, you can avoid this warning in a number of ways.
source to share
OK
var arr = [String]()
var i: Int64 = 0
if arr.count == 0 {
i = 1
} else {
i = arr.count + 1
}
indicates that the warning "+" is deprecated: Mixed type addition is deprecated. Please use explicit type conversion.
The reason is that arr.count and I are of different types . And this warning is correct. It has nothing with integer literal 1
this snippet gives you a warning too
var arr = [String]()
var i: Int64 = 0
if arr.count == 0 {
i = 1
} else {
i = 1
i += arr.count // here is the warning now
}
it won't compile, although it looks very similar to
var arr = [String]()
var i: Int64 = 0
if arr.count == 0 {
i = 1
} else {
let tmp = arr.count + 1
i = tmp
}
Hopefully we'll get an error message when we compose all these snippets in a future release.
source to share