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

RFC: required keyword arguments #25830

Merged
merged 5 commits into from
Feb 2, 2018
Merged

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Jan 31, 2018

This PR implements required keyword arguments (closes #5111):

f(; x) = ...

is sugar for

f(; x=throw(UndefKeywordError(:x))) = ...

@stevengj stevengj added the keyword arguments f(x; keyword=arguments) label Jan 31, 2018
Copy link
Sponsor Member

@StefanKarpinski StefanKarpinski left a comment

Choose a reason for hiding this comment

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

Cool! Looks good to me. Probably should get @JeffBezanson's review.

base/boot.jl Outdated
@@ -253,6 +254,9 @@ end
struct ArgumentError <: Exception
msg::AbstractString
end
struct UnassignedKeyword <: Exception
Copy link
Sponsor Member

@KristofferC KristofferC Jan 31, 2018

Choose a reason for hiding this comment

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

Should this end with Error or Exception for consistency with most (all) other errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I can do that. UnassignedKeywordError is a bit long. Maybe UndefKeywordError?

Copy link
Member

Choose a reason for hiding this comment

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

KeywordError? Or KeywordException?

Copy link
Member Author

Choose a reason for hiding this comment

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

We also have UndefVarError and UndefRefError, so UndefKeywordError seems consistent. KeywordError seems too vague, since there are other ways you could get errors from keyword args.

Copy link
Member

Choose a reason for hiding this comment

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

But UndefKeywordError feels a bit like something that would be thrown for cases like

foo(;a = 1) = a
foo(b=2)

Copy link
Sponsor Member

@StefanKarpinski StefanKarpinski Jan 31, 2018

Choose a reason for hiding this comment

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

I would consider that an unexpected keyword argument error. "undef" is pretty consistently used to mean something that is used as a value without having been given one.

@ararslan
Copy link
Member

ararslan commented Feb 1, 2018

Looks like you'll need

--- a/test/syntax.jl
+++ b/test/syntax.jl
@@ -500,10 +500,6 @@ let m_error, error_out, filename = Base.source_path()
     error_out = sprint(showerror, m_error)
     @test startswith(error_out, "ArgumentError: invalid type for argument number 1 in method definition for method_c6 at $filename:")

-    m_error = try @eval method_c6(A; B) = 3; catch e; e; end
-    error_out = sprint(showerror, m_error)
-    @test error_out == "syntax: keyword argument \"B\" needs a default value"
-
     # issue #20614
     m_error = try @eval foo(types::NTuple{N}, values::Vararg{Any,N}, c) where {N} = nothing; catch e; e; end
     error_out = sprint(showerror, m_error)

since that's now a valid method definition.

@stevengj
Copy link
Member Author

stevengj commented Feb 1, 2018

Removed the obsolete syntax test; hopefully tests pass now.

@stevengj
Copy link
Member Author

stevengj commented Feb 1, 2018

Yay, green CI. Will merge shortly (in a day or so) since it seems everyone approves.

@ararslan
Copy link
Member

ararslan commented Feb 1, 2018

Wow, CI is green across the board, nice work!

@stevengj
Copy link
Member Author

stevengj commented Feb 2, 2018

@ararslan, yes, it is clearly the lack of required keyword arguments that has been the source of all of our CI unreliability. 😉

@stevengj stevengj merged commit 6eb7f3d into JuliaLang:master Feb 2, 2018
@stevengj stevengj deleted the requiredkw branch February 2, 2018 13:04
@StefanKarpinski StefanKarpinski added this to the 1.0 milestone Feb 2, 2018
dourouc05 added a commit to dourouc05/Seleroute.jl that referenced this pull request Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keyword arguments f(x; keyword=arguments)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

required keyword arguments?
6 participants