Skip to content
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

confusing docs for range #31707

Closed
ssfrr opened this issue Apr 12, 2019 · 2 comments
Closed

confusing docs for range #31707

ssfrr opened this issue Apr 12, 2019 · 2 comments

Comments

@ssfrr
Copy link
Contributor

ssfrr commented Apr 12, 2019

As of 1.1 the docs for range say:

    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). One of
length or stop is required. If length, stop, and step are all specified, they
must agree.

Based on the docs it seems like range(1, 5) should be valid, but it throws the error that "at least one of length or step must be specified".

From the doc header it looks like step is specified by default, so it seems weird that range(1, 5) would be different from range(1, 5; step=1). Also it makes it look like stop can be specified either positionally or by keyword, so I'd think specifying positionally would satisfy that "One of length or stop is required".

Based on the discussion in #28708 this seems like the intended behavior, but it seems like allowing range(a, b) would be consistent with the docs and somewhat simpler as just meaning the same thing as a:b.

This confusion popped up in a recent PR in DSP.jl.

(@galenlynch)

@galenlynch
Copy link
Contributor

Also this is a bummer, even if I do understand why it's the case:

julia> typeof(1:2)
UnitRange{Int64}

julia> typeof(range(1, 2, step = 1))
StepRange{Int64,Int64}

In contrast, in Julia 0.7 (and 1.0?):

julia> typeof(range(1, 2))
┌ Warning: `range(start, length)` is deprecated, use `range(start, length=length)` instead.
│   caller = top-level scope at none:0
└ @ Core none:0
UnitRange{Int64}

This can make a big difference when trying to construct arrays with custom axes with packages like OffsetArrays.jl.

@sostock
Copy link
Contributor

sostock commented Jun 3, 2021

As of Julia 1.7, range(1, 5) is valid and equivalent to 1:5. The docstring has also been revised.

@sostock sostock closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants