Swift allows to define our own precedence groups to use for our custom operators. There is one thing I struggle to understand and that is boolean assignment property.
From The Swift Programming Language (Swift 3.1):
The assignment of a precedence group specifies the precedence of an operator when used in an operation that includes optional chaining. When set to
true, an operator in the corresponding precedence group uses the same grouping rules during optional chaining as the assignment operators from the standard library. Otherwise, when set tofalseor omitted, operators in the precedence group follows the same optional chaining rules as operators that don’t perform assignment.
It doesn't say much about the exact behavior of assignment property. What is the difference between assignment and non-assignment operators in optional chaining?
I defined ++= (behaving like +=) and ++ (behaving like +) operators along with AssignmentTesting struct for testing.
precedencegroup AssignmentTrue {
assignment: true
}
precedencegroup AssignmentFalse {
assignment: false
}
infix operator ++=: AssignmentTrue
infix operator ++: AssignmentFalse
extension Int {
static func ++= (left: inout Int, right: Int) {
left += right
}
static func ++ (left: Int, right: Int) -> Int {
return left + right
}
}
struct AssignmentTesting { var number = 0 }
var assignmentTesting: AssignmentTesting? = AssignmentTesting()
assignmentTesting?.number ++= 3 // assigns 0 + 3 to assignmentTesting.number
assignmentTesting!.number ++ 5 // returns 3 + 5
assignmentTesting?.number // == 3
How do assignment: true and assignment: false influence these behaviors? Any explanation is highly welcomed.