-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Factor MethodList representation by optional arguments #44434
base: master
Are you sure you want to change the base?
Conversation
8abcced
to
9919b06
Compare
CI failure looks unrelated. Just a few more examples to illustrate: Multiple optional argumentsjulia> foo(a, b=12, c=""; kwarg) = nothing;
julia> methods(foo)
# 3 methods for generic function "foo" from Main:
[1:3] foo(a, [b, c]; kwarg)
@ REPL[1]:1 Non-consecutive factored methodsjulia> bar(x, y::Integer) = 2;
julia> bar(a, b=5) = 1;
julia> bar( #TAB
bar(x, y::Integer) @ Main REPL[3]:1
bar(a, [b]) @ Main REPL[4]:1
julia> methods(bar)
# 3 methods for generic function "bar" from Main:
[1,3] bar(a, [b])
@ REPL[4]:1
[2] bar(x, y::Integer)
@ REPL[3]:1 Note that the order of methods in the Optional arguments and varargsjulia> qux(a, b=12, args...) = b;
julia> methods(qux)
# 2 methods for generic function "qux" from Main:
[1:2] qux(a, [b], args...)
@ REPL[6]:1 |
I suppose one of the questions for this is whether we want to be accurate or simplified (like here). The actual lowering uses kwargs, and accepts any list of them, so
|
So the question is between hiding the internal methods created when lowering the definition of I think I still prefer not to show those internal methods. Even though there may be an internal method for However, I understand that this representation may also be queried by devs who would prefer to have the most accurate representation. At the same time, I would expect them to know how to extract the desired methods "by hand" from |
The current representation can lead to inaccurate exception info in some cases like this:
|
9919b06
to
872f646
Compare
872f646
to
7d7545b
Compare
Rebased on top of #45069. I incidentally expanded support of that PR for the
|
Up |
cfa5c0d
to
821dd43
Compare
821dd43
to
14006a4
Compare
14006a4
to
6c3174c
Compare
When defining a function with both optional and keyword arguments, the representation of the resulting methods do not show the keyword arguments, except for the last one. For example, there is no
foo(a; kwarg)
method appearing in the following list, although callingfoo(3; kwarg=6)
is valid:I suppose this comes from the actual implementation of method dispatch with keyword arguments, with the kwsorter and stuff, and there are good reasons why those methods are defined like that. Without changing any of these internals thus, this PR changes the representation of the
MethodList
created when callingmethods(foo)
, in order to show a "factored method" closer to the definition:This is done by converting the
MethodList
to the newFactoredMethodList
type upon callingshow
, so theMethodList
itself is untouched. You can still access the previous representation by looking at the actual list of methods:The detection of "factored method" definitions works by comparing the locations of the method definitions, as well as their argument names and types. It's not perfect since you could define methods at the same position through macros or
@eval
, but I don't think it's too bad since there is no loss of information anyway, all previously represented methods will still be represented (in a more compact way possibly).This is also ported to REPL completions, which was the initial reason for this PR (see #43536):