Skip to content

Commit 1ece4b7

Browse files
committed
allow specifying multiple modules
1 parent d8e311f commit 1ece4b7

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

base/reflection.jl

+9-3
Original file line numberDiff line numberDiff line change
@@ -849,14 +849,19 @@ Return the method table for `f`.
849849
850850
If `types` is specified, return an array of methods whose types match.
851851
If `module` is specified, return an array of methods defined in this module.
852+
A list of modules can also be specified as an array or tuple.
852853
"""
853-
function methods(@nospecialize(f), @nospecialize(t), mod::Union{Module,Nothing}=nothing)
854+
function methods(@nospecialize(f), @nospecialize(t),
855+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}}}=()))
856+
if mod isa Module
857+
mod = (mod,)
858+
end
854859
if isa(f, Core.Builtin)
855860
throw(ArgumentError("argument is not a generic function"))
856861
end
857862
t = to_tuple_type(t)
858863
world = typemax(UInt)
859-
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module == mod],
864+
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if isempty(mod) || m[3].module in mod],
860865
typeof(f).name.mt)
861866
end
862867

@@ -871,7 +876,8 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
871876
return MethodList(Method[m[3] for m in ms], typeof(f).name.mt)
872877
end
873878

874-
function methods(@nospecialize(f), mod::Union{Module,Nothing}=nothing)
879+
function methods(@nospecialize(f),
880+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}}}=()))
875881
# return all matches
876882
return methods(f, Tuple{Vararg{Any}}, mod)
877883
end

test/reflection.jl

+27
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,30 @@ end
892892
@test nameof(:) === :Colon
893893
@test nameof(Core.Intrinsics.mul_int) === :mul_int
894894
@test nameof(Core.Intrinsics.arraylen) === :arraylen
895+
896+
module TestMod33403
897+
f(x) = 1
898+
f(x::Int) = 2
899+
900+
module Sub
901+
using ..TestMod33403
902+
TestMod33403.f(x::Char) = 3
903+
end
904+
end
905+
906+
@testset "methods with module" begin
907+
using .TestMod33403: f
908+
@test length(methods(f)) == 3
909+
@test length(methods(f, (Int,))) == 1
910+
911+
@test length(methods(f, TestMod33403)) == 2
912+
@test length(methods(f, (TestMod33403,))) == 2
913+
@test length(methods(f, [TestMod33403])) == 2
914+
@test length(methods(f, (Int,), TestMod33403)) == 1
915+
@test length(methods(f, (Int,), (TestMod33403,))) == 1
916+
917+
@test length(methods(f, TestMod33403.Sub)) == 1
918+
@test length(methods(f, (TestMod33403.Sub,))) == 1
919+
@test length(methods(f, (Char,), TestMod33403.Sub)) == 1
920+
@test length(methods(f, (Int,), TestMod33403.Sub)) == 0
921+
end

0 commit comments

Comments
 (0)