Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Standard library changes
------------------------

* `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]).
* `range` now supports the `range(start, stop)` and `range(start, stop, length)` methods ([#39228]).
* `range` now supports `start` as an optional keyword argument ([#38041]).
* `islowercase` and `isuppercase` are now compliant with the Unicode lower/uppercase categories ([#38574]).
* `iseven` and `isodd` functions now support non-`Integer` numeric types ([#38976]).
Expand Down
24 changes: 6 additions & 18 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function _colon(start::T, step, stop::T) where T
end

"""
range(start, stop, length)
range(start, stop; length, step)
range(start; length, stop, step)
range(;start, length, stop, step)
Expand Down Expand Up @@ -96,34 +97,21 @@ julia> range(1, 3.5, step=2)
Special care is taken to ensure intermediate values are computed rationally.
To avoid this induced overhead, see the [`LinRange`](@ref) constructor.

Both `start` and `stop` may be specified as either a positional or keyword arguments.
If both are specified as positional arguments, one of `step` or `length` must also be provided.

!!! compat "Julia 1.1"
`stop` as a positional argument requires at least Julia 1.1.

!!! compat "Julia 1.7"
`start` as a keyword argument requires at least Julia 1.7.

!!! compat "Julia 1.7"
the versions without keyword arguments require at least Julia 1.7.
"""
function range end

range(start; stop=nothing, length::Union{Integer,Nothing}=nothing, step=nothing) =
_range(start, step, stop, length)

function range(start, stop; length::Union{Integer,Nothing}=nothing, step=nothing)
# For code clarity, the user must pass step or length
# See https://github.com/JuliaLang/julia/pull/28708#issuecomment-420034562
if step === length === nothing
msg = """
Neither `step` nor `length` was provided. To fix this do one of the following:
* Pass one of them
* Use `$(start):$(stop)`
* Use `range($start, stop=$stop)`
"""
throw(ArgumentError(msg))
end
_range(start, step, stop, length)
end
range(start, stop; length::Union{Integer,Nothing}=nothing, step=nothing) = _range(start, step, stop, length)
range(start, stop, length::Integer) = _range(start, nothing, stop, length)

range(;start=nothing, stop=nothing, length::Union{Integer, Nothing}=nothing, step=nothing) =
_range(start, step, stop, length)
Expand Down
6 changes: 4 additions & 2 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
r = 4:9
@test r === range(start=first(r), stop=last(r) )
@test r === range(start=first(r), length=length(r))
# the next one uses ==, because it changes the eltype
@test r == range(start=first(r), stop=last(r), length=length(r))
@test r === range( stop=last(r), length=length(r))
@test r === range(first(r), last(r))
# the next ones use ==, because it changes the eltype
@test r == range(first(r), last(r), length(r))
@test r == range(start=first(r), stop=last(r), length=length(r))
Copy link
Contributor

@mkitti mkitti Jan 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@test r === range( stop=last(r), length=length(r))
@test r === range(first(r), last(r))
# the next ones use ==, because it changes the eltype
@test r == range(first(r), last(r), length(r))
@test r == range(start=first(r), stop=last(r), length=length(r))
@test r === range( stop=last(r), length=length(r))
@test r === range( first(r), last(r) )
# the next ones use ==, because it changes the eltype
@test r == range( first(r), last(r), length(r))
@test r == range(start=first(r), stop=last(r), length=length(r))

@jw3126 did a nice job of lining up similar aspects from each line. Perhaps we should maintain that style.

end
end

Expand Down