Skip to content

Commit 4ffefa8

Browse files
committed
Add implementation notes for SE-0225
1 parent 52b899b commit 4ffefa8

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

proposals/0225-binaryinteger-iseven-isodd-ismultiple.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Proposal: [SE-0225](0225-binaryinteger-iseven-isodd-ismultiple.md)
44
* Authors: [Robert MacEachern](https://github.com/robmaceachern), [Micah Hansonbrook](https://github.com/SiliconUnicorn)
55
* Review Manager: [John McCall](https://github.com/rjmccall)
6-
* Status: **Accepted with modifications**
6+
* Status: **Implemented (Swift 5)** (with modifications, see Implementation Notes)
77
* Implementation: [apple/swift#18689](https://github.com/apple/swift/pull/18689)
88
* 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)
99

@@ -155,6 +155,22 @@ On the other hand there was some unscientific analysis that indicated that even/
155155

156156
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.
157157

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+
158174
## Appendix
159175

160176
### Other example uses in code (and beyond)

0 commit comments

Comments
 (0)