|
3 | 3 | * Proposal: [SE-0225](0225-binaryinteger-iseven-isodd-ismultiple.md)
|
4 | 4 | * Authors: [Robert MacEachern](https://github.com/robmaceachern), [Micah Hansonbrook](https://github.com/SiliconUnicorn)
|
5 | 5 | * Review Manager: [John McCall](https://github.com/rjmccall)
|
6 |
| -* Status: **Accepted with modifications** |
| 6 | +* Status: **Implemented (Swift 5)** (with modifications, see Implementation Notes) |
7 | 7 | * Implementation: [apple/swift#18689](https://github.com/apple/swift/pull/18689)
|
8 | 8 | * Review: [Discussion thread](https://forums.swift.org/t/se-0225-adding-iseven-isodd-ismultiple-to-binaryinteger/15382), [Announcement thread](https://forums.swift.org/t/accepted-with-modifications-se-0225-adding-iseven-isodd-ismultiple-to-binaryinteger/15689)
|
9 | 9 |
|
@@ -155,6 +155,22 @@ On the other hand there was some unscientific analysis that indicated that even/
|
155 | 155 |
|
156 | 156 | The authors decided that both were worthy of including in the proposal. Odd and even numbers have had special [shorthand labels](http://mathforum.org/library/drmath/view/65413.html) for thousands of years and are used frequently enough to justify the small additional weight `isEven/isOdd` would add to the standard library. `isMultiple` will greatly improve clarity and readability for arbitrary divisibility checks and also avoid potentially surprising `%` operator semantics with negative values.
|
157 | 157 |
|
| 158 | +## Implementation Notes |
| 159 | + |
| 160 | +Only `isMultiple(of:)` was approved during review, so the final implementation does not |
| 161 | +include `isEven` or `isOdd`. A default implementation is provided for integers conforming to |
| 162 | +`FixedWidthInteger`; types conforming to `BinaryInteger` but not `FixedWidthInteger` |
| 163 | +will need to provide their own implementation. For *most* such types, the following will be |
| 164 | +correct: |
| 165 | +```swift |
| 166 | +func isMultiple(of other: Self) -> Bool { |
| 167 | + if other == 0 { return self == 0 } |
| 168 | + return self % other == 0 |
| 169 | +} |
| 170 | +``` |
| 171 | +but pay careful attention to the behavior around any edge cases where the result of `%` may |
| 172 | +not be representable in your type. |
| 173 | + |
158 | 174 | ## Appendix
|
159 | 175 |
|
160 | 176 | ### Other example uses in code (and beyond)
|
|
0 commit comments