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

Add range with keyword arguments #511

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.IOBuffer` supporting keyword arguments ([#25873]).

* `Compat.range` supporting keyword arguments ([25896]).
Copy link
Member

Choose a reason for hiding this comment

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

Needs # in issue link.


## Renaming

Expand Down Expand Up @@ -370,6 +371,8 @@ Currently, the `@compat` macro supports the following syntaxes:
* `DevNull`, `STDIN`, `STDOUT` and `STDERR` are now `devnull`, `stdin`, `stdout` and
`stderr` respectively ([#25959]).

* `LinSpace` is now `LinRange` ([#25896]).

## New macros

* `@__DIR__` has been added ([#18380])
Expand Down Expand Up @@ -574,5 +577,6 @@ includes this fix. Find the minimum version from there.
[#25780]: https://github.com/JuliaLang/julia/issues/25780
[#25819]: https://github.com/JuliaLang/julia/issues/25819
[#25873]: https://github.com/JuliaLang/julia/issues/25873
[#25896]: https://github.com/JuliaLang/julia/issues/25896
[#25990]: https://github.com/JuliaLang/julia/issues/25990
[#26089]: https://github.com/JuliaLang/julia/issues/26089
29 changes: 29 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,35 @@ if VERSION < v"0.7.0-DEV.3734"
end
end

@static if VERSION < v"0.7.0-DEV.3986"
const LinRange = Base.LinSpace
export LinRange

function range(start; step=nothing, stop=nothing, length=nothing)
have_step = step !== nothing
have_stop = stop !== nothing
have_length = length !== nothing

if !(have_stop || have_length)
throw(ArgumentError("At least one of `length` or `stop` must be specified"))
elseif have_step && have_stop && have_length
throw(ArgumentError("Too many arguments specified; try passing only one of `stop` or `length`"))
elseif start === nothing
throw(ArgumentError("Can't start a range at `nothing`"))
end

if have_stop && !have_length
return have_step ? start:step:stop : start:stop
Copy link
Member

Choose a reason for hiding this comment

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

Need parens here. Should be have_step ? (start:step:stop) : (start:stop) but actually parses as have_step ? start : ((step:stop:start):stop)

Copy link
Member Author

Choose a reason for hiding this comment

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

image

elseif have_length && !have_stop
return have_step ? Base.range(start, step, length) : Base.range(start, length)
elseif !have_step
return linspace(start, stop, length)
end
end
else
import Base: range, LinRange
end

include("deprecated.jl")

end # module Compat
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,14 @@ let buf = Compat.IOBuffer(sizehint=20)
@test String(take!(buf)) == "Hello world.\n"
end

# 0.7.0-DEV.3986
@test_throws ArgumentError Compat.range(1)
@test_throws ArgumentError Compat.range(nothing)
@test_throws ArgumentError Compat.range(1, step=1)
@test_throws ArgumentError Compat.range(1, step=1, stop=4, length=3)
@test Compat.range(2, step=2, stop=8) == 2:2:8
@test Compat.range(2, stop=8) == 2:8
@test Compat.range(2, step=2, length=8) == 2:2:16
@test Compat.range(1.0, stop=2.0, length=3) == 1.0:0.5:2.0

nothing