@@ -1321,31 +1321,44 @@ function MethodList(mt::Core.MethodTable)
1321
1321
return MethodList (ms, mt)
1322
1322
end
1323
1323
1324
+ function matches_to_methods (ms:: Array{Any,1} , mt:: Core.MethodTable , mod)
1325
+ # Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
1326
+ ms = Method[(ms[i]:: Core.MethodMatch ). method for i in 1 : length (ms)]
1327
+ # Remove shadowed methods with identical type signatures
1328
+ prev = nothing
1329
+ filter! (ms) do m
1330
+ l = prev
1331
+ repeated = (l isa Method && m. sig == l. sig)
1332
+ prev = m
1333
+ return ! repeated
1334
+ end
1335
+ # Remove methods not part of module (after removing shadowed methods)
1336
+ mod === nothing || filter! (ms) do m
1337
+ return parentmodule (m) ∈ mod
1338
+ end
1339
+ return MethodList (ms, mt)
1340
+ end
1341
+
1324
1342
"""
1325
1343
methods(f, [types], [module])
1326
1344
1327
1345
Return the method table for `f`.
1328
1346
1329
1347
If `types` is specified, return an array of methods whose types match.
1330
1348
If `module` is specified, return an array of methods defined in that module.
1331
- A list of modules can also be specified as an array.
1349
+ A list of modules can also be specified as an array or set .
1332
1350
1333
1351
!!! compat "Julia 1.4"
1334
1352
At least Julia 1.4 is required for specifying a module.
1335
1353
1336
1354
See also: [`which`](@ref), [`@which`](@ref Main.InteractiveUtils.@which) and [`methodswith`](@ref Main.InteractiveUtils.methodswith).
1337
1355
"""
1338
1356
function methods (@nospecialize (f), @nospecialize (t),
1339
- mod:: Union{Tuple{Module},AbstractArray{Module},Nothing} = nothing )
1357
+ mod:: Union{Tuple{Module},AbstractArray{Module},AbstractSet{Module}, Nothing} = nothing )
1340
1358
world = get_world_counter ()
1341
1359
world == typemax (UInt) && error (" code reflection cannot be used from generated functions" )
1342
- # Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
1343
- ms = Method[]
1344
- for m in _methods (f, t, - 1 , world):: Vector
1345
- m = m:: Core.MethodMatch
1346
- (mod === nothing || parentmodule (m. method) ∈ mod) && push! (ms, m. method)
1347
- end
1348
- MethodList (ms, typeof (f). name. mt)
1360
+ ms = _methods (f, t, - 1 , world):: Vector{Any}
1361
+ return matches_to_methods (ms, typeof (f). name. mt, mod)
1349
1362
end
1350
1363
methods (@nospecialize (f), @nospecialize (t), mod:: Module ) = methods (f, t, (mod,))
1351
1364
@@ -1355,12 +1368,12 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
1355
1368
world == typemax (UInt) && error (" code reflection cannot be used from generated functions" )
1356
1369
min = RefValue {UInt} (typemin (UInt))
1357
1370
max = RefValue {UInt} (typemax (UInt))
1358
- ms = _methods_by_ftype (tt, nothing , - 1 , world, true , min, max, Ptr {Int32} (C_NULL )):: Vector
1359
- return MethodList (Method[(m :: Core.MethodMatch ) . method for m in ms] , typeof (f). name. mt)
1371
+ ms = _methods_by_ftype (tt, nothing , - 1 , world, true , min, max, Ptr {Int32} (C_NULL )):: Vector{Any}
1372
+ return matches_to_methods (ms , typeof (f). name. mt, nothing )
1360
1373
end
1361
1374
1362
1375
function methods (@nospecialize (f),
1363
- mod:: Union{Module,AbstractArray{Module},Nothing} = nothing )
1376
+ mod:: Union{Module,AbstractArray{Module},AbstractSet{Module}, Nothing} = nothing )
1364
1377
# return all matches
1365
1378
return methods (f, Tuple{Vararg{Any}}, mod)
1366
1379
end
0 commit comments