-
Notifications
You must be signed in to change notification settings - Fork 441
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
[Bug or FeatureRequest]: collection.rotate(toStartAt: Int) crashes with negative argument #239
Comments
Hi Heiko, the argument to Note that not all collections with integer indices start at |
@xwu Thanks for your remark - given some thoughts on your response, I can follow your argumentation (especially with setting a start index instead of an offset for rotation. I left my original comment for reference purpose only.
Swift as a language is designed to be safe to use (strongly and statically typed language, optionals, basically no manual memory management, etc). It's really hard to "shoot yourself in the foot" with Swift unless you purposely opt-in into such a behaviour. In my opinion, frameworks / packages should follow the same idea of safety and should fail gracefully. A fatal error seems a bit to harsh of a reaction for a function that simply could return "sorry, can't do that" (again, more difficult for an inplace change of order of elements) - A possible solution (sorry again, I mixed up an encountered issue with possible solution) would be to interpret a "negative index" as an index starting from the end (seems to be reasonable to me)
|
Replace this paragraph with a short description of the incorrect incorrect behavior. If this is a regression, please note the last version that the behavior was correct in addition to your current version.
Swift Algorithms version:
1.2.0
(= main branch currently)Swift version:
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Checklist
main
branch of this packageSteps to Reproduce
calling .rotate(toStartAt: ) with a negative number causes a run-time error
Expected behavior
This function shouldn't crash. Considering what .rotate() does (positive values rotate elements to the left) being called with a negative value should rotate to the right.
Another option would be to only allow for positive values as arguments, e.g.
UInt
as parameter.Actual behavior
Code crashes with an fatal error:
Swift/arm64e-apple-macos.swiftinterface:16236: Fatal error: Range requires lowerBound <= upperBound
Kind regards,
Heiko
The text was updated successfully, but these errors were encountered: