Skip to content

Add implementation notes for SE-0225 #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion proposals/0225-binaryinteger-iseven-isodd-ismultiple.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Proposal: [SE-0225](0225-binaryinteger-iseven-isodd-ismultiple.md)
* Authors: [Robert MacEachern](https://github.com/robmaceachern), [Micah Hansonbrook](https://github.com/SiliconUnicorn)
* Review Manager: [John McCall](https://github.com/rjmccall)
* Status: **Accepted with modifications**
* Status: **Implemented (Swift 5)** (with modifications, see Implementation Notes)
* Implementation: [apple/swift#18689](https://github.com/apple/swift/pull/18689)
* 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)

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

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.

## Implementation Notes

Only `isMultiple(of:)` was approved during review, so the final implementation does not
include `isEven` or `isOdd`. A default implementation is provided for integers conforming to
`FixedWidthInteger`; types conforming to `BinaryInteger` but not `FixedWidthInteger`
will need to provide their own implementation. For *most* such types, the following will be
correct:
```swift
func isMultiple(of other: Self) -> Bool {
if other == 0 { return self == 0 }
return self % other == 0
}
```
but pay careful attention to the behavior around any edge cases where the result of `%` may
not be representable in your type.

## Appendix

### Other example uses in code (and beyond)
Expand Down