Thanks for all the great feedback everybody. Overall the reaction seems pretty positive. I’ll try to sum up the discussion so far.
isDivisible
A couple of people brought up isDivisible(by n: Self) -> Bool
as an alternative and/or addition to isEven/isOdd
. I think this could be a useful addition to BinaryInteger
but it probably deserves a fuller discussion with regards to the motivation criteria. It is certainly more general but in my opinion it loses some expressiveness and discoverability.
buttonSave.isEnabled = photos.count.isOdd
buttonSave.isEnabled = !photos.count.isDivisible(by: 2)
I’m not familiar with the details, but it looks like there is decent support for key path getter promotion which would make working with properties like isEven/isOdd
in a point-free fashion quite nice vs a more general isDivisible(by:)
.
xs.filter(\.isEven)
I will include it in the Alternatives section of the proposal but if you feel very strongly about it, it may be worth starting a separate pitch thread for it!
Property declaration on protocol vs extension
It does seem like isEven/isOdd
should be defined directly on the BinaryInteger
protocol and not on an extension. Is there any argument against doing that?
More examples of isEven/isOdd in the wild
There are some more great examples of the %
operator being used incorrectly to determine parity, but it’d be great to get a few more examples of how you’ve used custom isEven/isOdd
properties or functions in your own code (or examples of code where you would have used it had it been available).
Digging through the archives
This one is more for fun. I was able to track down the Ruby Change Request (RCR) for Ruby’s even?
and odd?
methods all the way back from 2006 using the web archive. The proposal was short and the vote tally was positive, but it was interesting to see odd?
proposed as self % 2 == 1
which would be incorrect for negative integers in Swift due to differences in the %
operator implementations.