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

Ignoring unrecognized keyword arguments #8070

Closed
pwl opened this issue Aug 20, 2014 · 2 comments
Closed

Ignoring unrecognized keyword arguments #8070

pwl opened this issue Aug 20, 2014 · 2 comments

Comments

@pwl
Copy link
Contributor

pwl commented Aug 20, 2014

Is it possible to redistribute the keyword arguments to subsequent function calls? I tried the following code

julia> f1(;x1=1)=x1
f1 (generic function with 1 method)

julia> f2(;x2=2)=x2
f2 (generic function with 1 method)

julia> g(;args...)=f1(;args...)+f2(;args...)
g (generic function with 1 method)

julia> g(x1=1)
ERROR: unrecognized keyword argument "x1"
 in g at none:1

julia> g(x2=1)
ERROR: unrecognized keyword argument "x2"
 in g at none:1

I was expecting x1 to be passed to f1 and x2 to be passed to f2. By redefining f2 as below I concluded that only the keyword arguments accepted by all subsequent function calls are valid for g.

julia> f2(;x1=1,x2=2)=x2
f2 (generic function with 1 method)

julia> g(x1=1)
3

julia> g(x2=1)
ERROR: unrecognized keyword argument "x2"
 in g at none:1

Wouldn't it be more natural to just ignore the unrecognized keyword arguments? Here is a real-life example of how I was trying to use keyword arguments

function meshsolve(rhs,; args...)
    [...]
    mesh=prepare_mesh(rhs; args...)
    [...]
    solve(rhs, mesh; args...)
end

The function meshsolve solves the equation rhs=0 but it first tries to prepare a suitable mesh. Both prepare_mesh and solve can accept keyword arguments, but for obvious reasons these arguments are not identical for both functions.

@JeffBezanson
Copy link
Sponsor Member

Any function can choose to ignore unrecognized keywords by adding ; args.... Given such a simple workaround, it doesn't seem appropriate to automatically ignore unmatched keywords everywhere. I think the current design has it right.

Also having the ;args... as a Dict might help (#4916).

@pwl
Copy link
Contributor Author

pwl commented Aug 20, 2014

Well, that solves my problem:-). I was not aware about this trick. Thank you!

@pwl pwl closed this as completed Aug 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants