-
Notifications
You must be signed in to change notification settings - Fork 6
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
Unintuitive and inconsistent behavior in range functions #169
Comments
@ceedubs exclusive upper bound for int ranges is usually what's done in languages with 0-based indexing. So the valid indices of a list of size If we were to change this, I like the idea of offering both, and to help with change management, do several releases where there is no default and you have to write |
I feel like the exclusive upper bound for index-based access is kind of carry-over from C-like languages and maybe is just confusing for a language like Unison where you typically are using higher-level functions like Another option that I'm not sure that I like is a helper My list of priorities would be:
|
I agree that for newcomers, inclusive ranges make the most intuitive sense and are least surprising. But in almost every other language (notably both Python and JavaScript) ranges that are inclusive of the lower bound and exclusive of the upper bound are the norm. I have a little library with an https://share.unison-lang.org/@runarorama/intervals/code/releases/1.0.3/latest/types/Interval |
@runarorama ooh I like how explicit your |
The library inderdaad looks nice @runarorama! About the operators: is it syntactically possible in Unison to use |
Names with dots in them are possible, but problematic as it's also used as a namespace separator |
Problem/background
Many of the functions related to ranges in base are exclusive on the upper bound, which always makes me think a bit harder, and I suspect that it would be surprising to newcomers. For example, I think that most people looking at the code
Random.natIn 1 2
would expect it to generate1
or2
with equal probability, but in fact it will always generate1
.The exclusive upper bound is also problematic when you want your range to include the max value (example:
maxNat
). I don't think that there's any simple way to achieve this functionality with exclusive ranges.There are also some inconsistencies in base. For example
Random.natIn lower upper
is exclusive in upper butText.patterns.charRange
is inclusive in upper.Proposed solution
I think that ranges should generally be inclusive and that exclusive ranges should be explicit in the name. So
Random.natIn lower upper
would includeupper
andRandom.nat.range.closed lower upperExcluded
(or whatever the name would be) would excludeupperExcluded
.The text was updated successfully, but these errors were encountered: