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

allow passing a module to methods #33403

Merged
merged 5 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Standard library changes
* Sets are now displayed less compactly in the REPL, as a column of elements, like vectors
and dictionaries ([#33300]).

* `methods` now accepts passing a module (or a list thereof) to filter methods defined in it ([#33403]).

#### Libdl

#### LinearAlgebra
Expand Down
26 changes: 19 additions & 7 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -862,19 +862,29 @@ function MethodList(mt::Core.MethodTable)
end

"""
methods(f, [types])
methods(f, [types], [module])

Returns the method table for `f`.
Return the method table for `f`.

If `types` is specified, returns an array of methods whose types match.
If `types` is specified, return an array of methods whose types match.
If `module` is specified, return an array of methods defined in this module.
A list of modules can also be specified as an array or tuple.

!!! compat "Julia 1.4"
At least Julia 1.4 is required for specifying a module.
"""
function methods(@nospecialize(f), @nospecialize(t))
function methods(@nospecialize(f), @nospecialize(t),
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}},Nothing}=nothing))
if mod isa Module
mod = (mod,)
end
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
end
t = to_tuple_type(t)
world = typemax(UInt)
return MethodList(Method[m[3] for m in _methods(f, t, -1, world)], typeof(f).name.mt)
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module in mod],
typeof(f).name.mt)
end

methods(f::Core.Builtin) = MethodList(Method[], typeof(f).name.mt)
Expand All @@ -887,9 +897,11 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
ms = ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), tt, -1, 1, world, min, max)::Array{Any,1}
return MethodList(Method[m[3] for m in ms], typeof(f).name.mt)
end
function methods(@nospecialize(f))

function methods(@nospecialize(f),
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}},Nothing}=nothing))
# return all matches
return methods(f, Tuple{Vararg{Any}})
return methods(f, Tuple{Vararg{Any}}, mod)
end

function visit(f, mt::Core.MethodTable)
Expand Down
27 changes: 27 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,30 @@ end
@test nameof(:) === :Colon
@test nameof(Core.Intrinsics.mul_int) === :mul_int
@test nameof(Core.Intrinsics.arraylen) === :arraylen

module TestMod33403
f(x) = 1
f(x::Int) = 2

module Sub
import ..TestMod33403: f
f(x::Char) = 3
end
end

@testset "methods with module" begin
using .TestMod33403: f
@test length(methods(f)) == 3
@test length(methods(f, (Int,))) == 1

@test length(methods(f, TestMod33403)) == 2
@test length(methods(f, (TestMod33403,))) == 2
@test length(methods(f, [TestMod33403])) == 2
@test length(methods(f, (Int,), TestMod33403)) == 1
@test length(methods(f, (Int,), (TestMod33403,))) == 1

@test length(methods(f, TestMod33403.Sub)) == 1
@test length(methods(f, (TestMod33403.Sub,))) == 1
@test length(methods(f, (Char,), TestMod33403.Sub)) == 1
@test length(methods(f, (Int,), TestMod33403.Sub)) == 0
end