-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Doc PR to clarify range usage #37875
Conversation
The current documentation for `range` is at worse wrong and at best misleading. This documentation change clarifies the usage of `range`.
|
||
Given a starting value, construct a range either by length or from `start` to `stop`, | ||
optionally with a given step (defaults to 1, a [`UnitRange`](@ref)). | ||
One of `length` or `stop` is required. If `length`, `stop`, and `step` are all specified, they must agree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
length
,stop
, andstep
are all specified, they must agree.
length
, stop
, and step
cannot be all specified:
Lines 166 to 167 in 01d89c6
_range(start::Real, step::Real, stop::Real, length::Integer) = # range(a, step=s, stop=b, length=l) | |
throw(ArgumentError("Too many arguments specified; try passing only one of `stop` or `length`")) |
_range(start::Real, step::Real, stop::Real, length::Integer) = # range(a, step=s, stop=b, length=l)
throw(ArgumentError("Too many arguments specified; try passing only one of `stop` or `length`"))
range(start[, stop]; length, stop, step=1) | ||
|
||
Given a starting value, construct a range either by length or from `start` to `stop`, | ||
optionally with a given step (defaults to 1, a [`UnitRange`](@ref)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step
does not default to 1 when length
is specified.
step
does not default to 1 when stop
is a positional argument. range(1,5)
does not work but range(1,5, step=1)
does.
The only time step
defaults to 1 is when using stop
as a positional argument: range(1,stop=5)
* `start`, `length`, and `stop` must be integers to get a `UnitRange` * A `step` of 1 does not result in a `UnitRange`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much appreciated!
base/range.jl
Outdated
# The following cause an ArgumentError to be thrown. | ||
range(start, stop) # Must specify either length or step | ||
range(start; step) # Cannot specify step alone | ||
range(start, stop; length, step) # Cannot specify all of stop, length, and step | ||
range(start; stop, length, step) # Cannot specify all of stop, length, and step | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should always put the one-or-two-line description right after the function signature.
This can be moved backward in the Examples section.
base/range.jl
Outdated
julia> try range(1, 5) catch e println(e) end | ||
ArgumentError("At least one of `length` or `step` must be specified") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alongside with the previous one comment, this could just be
# Must specify either length or step
julia> range(1, 5)
ERROR: ArgumentError: At least one of `length` or `step` must be specified
And the doctest only match the starts (if I get it correctly). For example, in OffsetArrays: https://github.com/JuliaArrays/OffsetArrays.jl/blob/87666ee004664282370122ba68e7467c13988b38/src/axes.jl#L12-L24
Reference: https://juliadocs.github.io/Documenter.jl/dev/man/doctests/#Exceptions
range doc: grammatical and formatting corrections Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
Could most of this be part of Extended Help, I think it's called, the lower section shown by An attempt:
|
A help / extended help split could work. I really have an issue with stating that
I wondering if specifying that is too much detail. I think we should only mention I also removed the reference to the missing parameter being calculated. No additional calculations are necessarily done in many circumstances. We should also minimally explain the keyword arguments. |
I guess there's no way to specify what this function does in one signature line, the rules are more complicated than that. In three lines you could say:
But I suppose the spirit in which to take the existing line is that it isn't code, it's meant to be explained in english below, and exists mostly to remind you what the possible keywords are (e.g. does Julia use
Yes, my writing
The core idea here is, I think, that there are 4 unknowns and 1 equation, so you have to provide 3 to uniquely specify the answer. If you provide 4, there is almost always no solution, hence the error. Maybe the docstring should write the equation somewhere: |
How about this, to stress the 4 unknowns view, and keep things compact: [Edited]
|
That's not accurate. I incorporated your three line proposal. |
Oh right,
This is not true, e.g. |
I removed the mention of I structured the Extended help section and added the equation express there. |
Looks like Thank you, @dkarrasch, @johnnychen94, and @mcabbott for helping to put this together. |
Closing due to pending changes in #38041 |
The current documentation for
range
is at worse wrong and at best misleading. This documentation change clarifies the usage ofrange
.