(.. ):
if john.residence?.numberOfRooms % 2 == 0
assignment: true. MultiplicationPrecedence:
precedencegroup MultiplicationPrecedence {
associativity:left
higherThan: AdditionPrecedence
}
You can create your own group of predecessors with assignment: true
precedencegroup AssignmentTruePrecedence {
assignment: true
associativity:left
higherThan:AdditionPrecedence
lowerThan:MultiplicationPrecedence
}
Now you can declare your own operator, assign it to the aforementioned priority group, and define a related function to perform the operation modulo.
infix operator %| : AssignmentTruePrecedence
extension Int {
static func %| (left: Int, right: Int)->Int {
return left % right
}
}
The string will work without errors with our new modulo operator:
if john.residence?.numberOfRooms %| 2 == 0
Turns out you donβt even have to declare a new statement, since re-allocating the modulo statement and assigning it to a new priority group seems to work!
infix operator % : AssignmentTruePrecedence
source
share