John Difool
John Difool

Reputation: 5702

Binary operator '+' can not be applied to two 'int2' operands. Need to use '&+'

I am trying to convert some code from Objective-C to Swift and running into this situation:

import simd

let a = int2(1, 0)
let b = int2(0, 1)

print(a + b) // COMPILER FAILS
print(a &+ b) // SUCCESS

Why is the purpose of changing a perfectly understandable syntax to this cryptic notation?

Upvotes: 2

Views: 461

Answers (2)

Scott Thompson
Scott Thompson

Reputation: 23701

From the Xcode 7 Release notes about Swift 2:

SIMD improvements: Integer vector types in the simd module now only support unchecked arithmetic with wraparound semantics using the &+, &-, and &* operators, in order to better support the machine model for vectors. The +, -, and * operators are unavailable on integer vectors, and Xcode automatically suggests replacing them with the wrapping operators.

(from https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc7_release_notes.html)

The unusual syntax is specifically there to make it obvious that the addition is being done with unchecked arithmetic.

Upvotes: 7

gnasher729
gnasher729

Reputation: 52538

Take out your Swift book and find the difference between a + b and a &+ b for plain integers.

Now you are using vectors. There is no SIMD operation that has the same semantics as +. It could be built, but then you would complain how slow it is. There is however a SIMD operation that has the same semantics as &+.

Upvotes: 1

Related Questions