Skip to content

Commit 354194a

Browse files
committed
allow specifying multiple modules
1 parent c3da3f9 commit 354194a

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
@@ -866,14 +866,19 @@ Return the method table for `f`.
866866
867867
If `types` is specified, return an array of methods whose types match.
868868
If `module` is specified, return an array of methods defined in this module.
869+
A list of modules can also be specified as an array or tuple.
869870
"""
870-
function methods(@nospecialize(f), @nospecialize(t), mod::Union{Module,Nothing}=nothing)
871+
function methods(@nospecialize(f), @nospecialize(t),
872+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}}}=()))
873+
if mod isa Module
874+
mod = (mod,)
875+
end
871876
if isa(f, Core.Builtin)
872877
throw(ArgumentError("argument is not a generic function"))
873878
end
874879
t = to_tuple_type(t)
875880
world = typemax(UInt)
876-
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module == mod],
881+
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if isempty(mod) || m[3].module in mod],
877882
typeof(f).name.mt)
878883
end
879884

@@ -888,7 +893,8 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
888893
return MethodList(Method[m[3] for m in ms], typeof(f).name.mt)
889894
end
890895

891-
function methods(@nospecialize(f), mod::Union{Module,Nothing}=nothing)
896+
function methods(@nospecialize(f),
897+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}}}=()))
892898
# return all matches
893899
return methods(f, Tuple{Vararg{Any}}, mod)
894900
end

test/reflection.jl

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

0 commit comments

Comments
 (0)