Skip to content

Commit

Permalink
Drop compat code for required keyword arguments from #586
Browse files Browse the repository at this point in the history
  • Loading branch information
martinholters committed Oct 8, 2019
1 parent 58adf43 commit ebc8817
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 38 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ Currently, the `@compat` macro supports the following syntaxes:
`CartesianRange` now has two type parameters, so using them as
fields in other `struct`s requires manual intervention.

* Required keyword arguments ([#25830]). For example, `@compat foo(; x, y)` makes `x` and `y` required keyword arguments: when calling `foo`, an error is thrown if `x` or `y` is not explicitly provided.

## Module Aliases

## New functions, macros, and methods
Expand Down
18 changes: 0 additions & 18 deletions src/compatmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@
using Base.Meta
export @compat

if !isdefined(Base, :UndefKeywordError)
struct UndefKeywordError <: Exception
kw
end
Base.showerror(io::IO, e::UndefKeywordError) = print(io, "UndefKeywordError: keyword argument $(e.kw) not assigned")
export UndefKeywordError
end

"Convert a functions symbol argument to the corresponding required keyword argument."
function symbol2kw(sym::Symbol)
Expr(:kw, sym, Expr(:call, throw, UndefKeywordError(sym)))
end
symbol2kw(arg) = arg

function _compat(ex::Expr)
if ex.head === :call
f = ex.args[1]
if !isdefined(Base, :UndefKeywordError) && length(ex.args) > 1 && isexpr(ex.args[2], :parameters)
params = ex.args[2]
params.args = map(symbol2kw, params.args)
end
elseif ex.head === :quote && isa(ex.args[1], Symbol)
# Passthrough
return ex
Expand Down
18 changes: 18 additions & 0 deletions test/old.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1277,3 +1277,21 @@ if VERSION < v"0.7.0-beta2.143"
@test_throws UndefKeywordError dropdims(a)
end
end

let
# test required keyword arguments
@compat func1() = 1
@test func1() == 1 # using the function works
@compat func2(x) = x
@test func2(3) == 3 # using the function works
@compat func3(;y) = y
@test func3(y=2) == 2 # using the function works
@test_throws UndefKeywordError func3()
@compat func4(x; z) = x*z
@test func4(2,z=3) == 6 # using the function works
@test_throws UndefKeywordError func4(2)
@compat func5(;x=1, y) = x*y
@test func5(y=3) == 3
@test func5(y=3, x=2) == 6
@test_throws UndefKeywordError func5(x=2)
end
18 changes: 0 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ let A = [1]
@test x == 1
end

let
# test required keyword arguments
@compat func1() = 1
@test func1() == 1 # using the function works
@compat func2(x) = x
@test func2(3) == 3 # using the function works
@compat func3(;y) = y
@test func3(y=2) == 2 # using the function works
@test_throws UndefKeywordError func3()
@compat func4(x; z) = x*z
@test func4(2,z=3) == 6 # using the function works
@test_throws UndefKeywordError func4(2)
@compat func5(;x=1, y) = x*y
@test func5(y=3) == 3
@test func5(y=3, x=2) == 6
@test_throws UndefKeywordError func5(x=2)
end

# Support for positional `stop`
@test Compat.range(0, 5, length = 6) == 0.0:1.0:5.0
@test Compat.range(0, 10, step = 2) == 0:2:10
Expand Down

0 comments on commit ebc8817

Please sign in to comment.