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

Better error needed for misuse of keyword args #9972

Closed
jdlangs opened this issue Jan 30, 2015 · 5 comments · Fixed by #24795
Closed

Better error needed for misuse of keyword args #9972

jdlangs opened this issue Jan 30, 2015 · 5 comments · Fixed by #24795
Labels
error handling Handling of exceptions by Julia or the user keyword arguments f(x; keyword=arguments)

Comments

@jdlangs
Copy link
Contributor

jdlangs commented Jan 30, 2015

I had a mental lapse and forgot to use the keyword argument name when invoking a function. I ended being baffled for a good five minutes before realizing what happened.

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

julia> f(;x=3)
4

julia> f(;3)
ERROR: BoundsError
 in anonymous at no file

This seems like something the parser should be able to pick up instantly.

@simonster simonster added the error handling Handling of exceptions by Julia or the user label Jan 30, 2015
@ivarne
Copy link
Member

ivarne commented Feb 2, 2015

+1 for making this a parser error.

Note that the ; is optional for calling a function with keyword arguments.

@JeffBezanson
Copy link
Member

Note the following is valid:

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

julia> p = :x=>1
:x=>1

julia> f(;p)
2

@jdlangs
Copy link
Contributor Author

jdlangs commented Feb 4, 2015

That does complicate it. Then maybe a couple runtime asserts can be added to check that whatever is passed is iterable with at least 2 elements, and the first is a symbol?

I realize it's probably a rarely seen case but the current error really does confuse way more than help to debug.

@jaboatman
Copy link

I just ran into this when generating function calls with named arguments from a macro.

A minimal example:

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

julia> macro m(named...)
           esc(:(f(;$(named...))))
       end

julia> @m x=1
ERROR: BoundsError
 in anonymous at no file

julia> @m :x=>1
2

julia> macroexpand(:(@m x=1))
:(f(; x = 1))

What makes the BoundsError particularly confusing is the apparently correct output from macroexpand.

@rofinn
Copy link
Contributor

rofinn commented Sep 20, 2015

+1. I guess a parsing error makes the most sense. Mostly, I'd just like the error to remind me what the signature is vs what it received, cause I'm dumb and have been confused by this a couple times.

@StefanKarpinski StefanKarpinski added this to the 0.6.0 milestone Aug 19, 2016
@JeffBezanson JeffBezanson modified the milestones: 0.6.x, 0.6.0 Jan 4, 2017
@JeffBezanson JeffBezanson modified the milestones: 1.x, 0.6.x May 17, 2017
@JeffBezanson JeffBezanson added the keyword arguments f(x; keyword=arguments) label Jul 20, 2017
JeffBezanson added a commit that referenced this issue Nov 26, 2017
JeffBezanson added a commit that referenced this issue Nov 26, 2017
JeffBezanson added a commit that referenced this issue Nov 26, 2017
JeffBezanson added a commit that referenced this issue Nov 27, 2017
JeffBezanson added a commit that referenced this issue Nov 27, 2017
ararslan pushed a commit that referenced this issue Dec 2, 2017
JeffBezanson added a commit that referenced this issue Dec 2, 2017
JeffBezanson added a commit that referenced this issue Dec 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user keyword arguments f(x; keyword=arguments)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants