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

required keyword arguments #586

Merged
merged 8 commits into from
Jul 18, 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
Next Next commit
required keyword arguments working
  • Loading branch information
Tim Kittel committed Jul 4, 2018
commit a9da698bd4892da82249165599f5f56828851316
13 changes: 13 additions & 0 deletions src/compatmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ else
new_style_typealias(ex) = false
end

"Provides the error message if no required keyword argument is given."
rka_error_message(sym) = string("RequiredKeywordArgumentError: `", sym, "` is a required keyword argument, please provide `", sym, " = ...`.")

"Convert a functions symbol argument to the corresponding required keyword argument."
function symbol2kw(sym::Symbol)
Expr(:kw, sym, Expr(:call, error, rka_error_message(sym)))
end
symbol2kw(arg) = arg
Copy link
Member

@stevengj stevengj Oct 19, 2018

Choose a reason for hiding this comment

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

This doesn't handle the case where the argument has a type. It should probably be:

arg2kw(arg, sym) = Expr(:kw, arg, Expr(:call, throw, UndefKeywordError(sym)))
symbol2kw(arg::Symbol) = arg2kw(arg, arg)
symbol2kw(arg::Expr) = isexpr(arg, :(::)) ? arg2kw(arg, arg.args[1]) : arg
symbol2kw(arg) = arg

Copy link
Member

Choose a reason for hiding this comment

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


function _compat(ex::Expr)
if ex.head === :call
f = ex.args[1]
if VERSION < v"0.6.0-dev.826" && length(ex.args) == 3 && # julia#18510
istopsymbol(withincurly(ex.args[1]), :Base, :Nullable)
ex = Expr(:call, f, ex.args[2], Expr(:call, :(Compat._Nullable_field2), ex.args[3]))
end
if length(ex.args) > 1 && isexpr(ex.args[2], :parameters)
params = ex.args[2]
params.args = map(symbol2kw, params.args)
end
elseif ex.head === :curly
f = ex.args[1]
if VERSION < v"0.6.0-dev.2575" #20414
Expand Down
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1856,4 +1856,35 @@ let
@test Compat.split(str, r"\.+:\.+"; limit=3, keepempty=true) == ["a","ba","cba.:.:.dcba.:."]
end

# 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
try
Copy link
Member

Choose a reason for hiding this comment

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

Instead of try-catch you can use @test_throws, for example

julia> f(;y) = y
f (generic function with 1 method)

julia> @test_throws UndefKeywordError f()
Test Passed
      Thrown: UndefKeywordError

func3()
catch e
@test isa(e, ErrorException)
@test e.msg == Compat.rka_error_message(:y)
end
@compat func4(x; z) = x*z
@test func4(2,z=3) == 6 # using the function works
try
func4(2)
catch e
@test isa(e, ErrorException)
@test e.msg == Compat.rka_error_message(:z)
end
@compat func5(;x=1, y) = x*y
@test func5(y=3) == 3
@test func5(y=3, x=2) == 6
try
func5(x=2)
catch e
@test isa(e, ErrorException)
@test e.msg == Compat.rka_error_message(:y)
end

nothing