Fix case of coerced zero numeric subtraction. #745
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before:
This works as expected:
But this does not:
A common case for the above would be when summing a list and subtracing a value from the sum. For instance, this works:
But if you happen to end up with an empty list and forget to handle it:
Uh oh! That's a bug!
Rather than requiring the developer to know to provide a default
Money.emptyvalue to.sum(which would fix the latter issue, but not the former one), this PR addresses the underlying issue of commutativeness being assumed during coercion even on subtraction. In reality, subtraction is not commutative, even if all we care about is the zero numeric case.After:
Once we apply the correct order on subtraction with a zero CoercedNumeric, we get a negative monetary amount: