如题,比如你自己定义了一个类型,并重载了三个函数(方法),那么你知道你定义的类型相关的方法有三个。如果你只知道这个类型名称,如何列出使用了这个类型的所有方法(函数)。
# code
如题,比如你自己定义了一个类型,并重载了三个函数(方法),那么你知道你定义的类型相关的方法有三个。如果你只知道这个类型名称,如何列出使用了这个类型的所有方法(函数)。
# code
help?> methodswith
search: methodswith
methodswith(typ[, module or function]; supertypes::Bool=false])
Return an array of methods with an argument of type typ.
The optional second argument restricts the search to a particular module or function (the default is all top-level
modules).
If keyword supertypes is true, also return arguments with a parent type of typ, excluding type Any.
julia> methodswith(Int64)
[1] tofirst(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:378
[2] tolast(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:390
[...]
julia> a = methodswith(Int64);
julia> a |> typeof
Array{Method,1}
julia> length(a)
190
julia> a[1:10]
[1] tofirst(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:378
[2] tolast(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:390
[3] tonext(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:336
[4] toprev(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:357
[5] middle(x::Union{Bool, Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) in Statistics at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Statistics\src\Statistics.jl:703
[6] rem(x::Int64, ::Type{Int8}) in Base at int.jl:454
[7] rem(x::Int64, ::Type{Int16}) in Base at int.jl:454
[8] rem(x::Int64, ::Type{Int32}) in Base at int.jl:454
[9] rem(x::Int64, ::Type{Int128}) in Base at int.jl:459
[10] rem(x::Int64, ::Type{UInt8}) in Base at int.jl:454
julia> a = methodswith(Int64);
julia> length(a)
190
julia> a[6:10]
[1] rem(x::Int64, ::Type{Int8}) in Base at int.jl:454
[2] rem(x::Int64, ::Type{Int16}) in Base at int.jl:454
[3] rem(x::Int64, ::Type{Int32}) in Base at int.jl:454
[4] rem(x::Int64, ::Type{Int128}) in Base at int.jl:459
[5] rem(x::Int64, ::Type{UInt8}) in Base at int.jl:454
julia> a[6].
ambig file isva name primary_world roots source
called generator line nargs pure sig specializations
deleted_world invokes module nospecialize resorted slot_syms unspecialized
julia> a[6].name
:rem
julia> a2 = unique(x->x.name, a);
julia> a2 |> length
67
julia> a2[1:10]
[1] tofirst(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:378
[2] tolast(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:390
[3] tonext(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:336
[4] toprev(dt::Dates.TimeType, dow::Int64) in Dates at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Dates\src\adjusters.jl:357
[5] middle(x::Union{Bool, Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) in Statistics at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Statistics\src\Statistics.jl:703
[6] rem(x::Int64, ::Type{Int8}) in Base at int.jl:454
[7] &(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:293
[8] *(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:54
[9] +(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:53
[10] -(x::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) in Base at int.jl:51
help?> methods
search: methods methodswith Method MethodError hasmethod
methods(f, [types])
Returns the method table for f.
If types is specified, returns an array of methods whose types match.
拿到函数方法的个数
julia> b = methods(+);
julia> b |> typeof
Base.MethodList
julia> b.m # 按 TAB
ms mt
julia> b.ms |> typeof
Array{Method,1}
julia> b.mt |> typeof
Core.MethodTable
julia> length(b.ms)
166
我是想知道有多少个f