Skip to content

Commit

Permalink
range: Remove one positional arg syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mkitti committed Jan 14, 2021
1 parent a03945e commit 4839fd8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
13 changes: 1 addition & 12 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ end
range(start, stop; length, step)
range(start; length, stop, step)
range(;start, length, stop, step)
range(stop)
Construct a specialized array with evenly spaced elements and optimized storage (an [`AbstractRange`](@ref)) from the arguments.
Mathematically a range is uniquely determined by any three of `start`, `step`, `stop` and `length`.
Expand Down Expand Up @@ -90,9 +89,6 @@ julia> range(stop=10, step=1, length=5)
julia> range(start=1, step=1, stop=10)
1:1:10
julia> range(5)
Base.OneTo(5)
julia> range(; length = 10)
Base.OneTo(10)
Expand Down Expand Up @@ -136,7 +132,7 @@ A `UnitRange` is not produced if `step` is provided even if specified as one.
function range end

range(start; stop=nothing, length::Union{Integer,Nothing}=nothing, step=nothing) =
_range_positional(start, step, stop, length)
_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
Expand All @@ -156,13 +152,6 @@ end
range(;start=nothing, stop=nothing, length::Union{Integer, Nothing}=nothing, step=nothing) =
_range(start, step, stop, length)

range(stop::Integer) = range_stop(stop)

_range_positional(stop::Any , step::Nothing, ::Nothing, len::Nothing) =
_range(nothing, nothing, stop, nothing) # One arg interpreted as `stop`, could be nothing
_range_positional(start::Any , step::Any , stop::Any, len::Any) =
_range(start, step, stop, len)

_range(start::Nothing, step::Nothing, stop::Nothing, len::Nothing) = range_error(start, step, stop, len)
_range(start::Nothing, step::Nothing, stop::Nothing, len::Any ) = range_length(len)
_range(start::Nothing, step::Nothing, stop::Any , len::Nothing) = range_stop(stop)
Expand Down
4 changes: 2 additions & 2 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
r = 1:5
o = Base.OneTo(5)
let start=first(r), step=step(r), stop=last(r), length=length(r)
@test o === range( length)
@test o === range(; stop )
@test o === range(; length)
@test r === range(; start, stop )
@test r === range(; stop, length)
# the next three lines uses ==, because it changes the eltype
@test r == range(; start, stop, length)
@test r == range(; start, step, length)
@test r == range(Float64(stop))
@test r == range(; stop=Float64(stop))
end
end
end
Expand Down Expand Up @@ -1458,6 +1457,7 @@ end

@testset "Bad range calls" begin
@test_throws ArgumentError range(nothing)
@test_throws ArgumentError range(5)
@test_throws ArgumentError range(1, step=4)
@test_throws ArgumentError range(; step=1, length=6)
@test_throws ArgumentError range(; step=2, stop=7.5)
Expand Down

0 comments on commit 4839fd8

Please sign in to comment.