logankilpatrick
logankilpatrick

Reputation: 14561

How to get all of the available methods in my code that have a specific name in Julia?

I know that there are a bunch of different implementations of a specific method in my code and I want to see a list of all of them. How can I see all the methods with a specific name?

Upvotes: 0

Views: 125

Answers (2)

HHFox
HHFox

Reputation: 329

A very convenient way of using methods() is to type the method name followed by a ( and then type TAB at the REPL, so for your example:

rand(

and then hit the TAB key. The list for rand( is very long though. If you continue writing your function call with arguments and hit TAB again, the list will be filtered according to all matching methods. In your case:

julia> rand(1,
rand(dims::Integer...) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:277
rand(X) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:258
rand(X, dims::Tuple{Vararg{Int64,N}} where N) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:280
rand(X, d::Integer, dims::Integer...) in Random at C:\Julia\Julia-1.4.0\share\julia\stdlib\v1.4\Random\src\Random.jl:283

EDIT:

Internally, Julia calls

methods(rand, (typeof(1), Any))

which would be the according filtering method in a piece of code (the docs unfortunatately do not include an example yet)

Upvotes: 1

logankilpatrick
logankilpatrick

Reputation: 14561

In Julia Base, there is a methods function which works as follows:

julia> methods(rand) # where rand is the the function name in question
# 68 methods for generic function "rand":
[1] rand(rd::Random.RandomDevice, sp::Union{Random.SamplerType{Bool}, Random.SamplerType{Int128}, Random.SamplerType{Int16}, Random.SamplerType{Int32}, Random.SamplerType{Int64}, Random.SamplerType{Int8}, Random.SamplerType{UInt128}, Random.SamplerType{UInt16}, Random.SamplerType{UInt32}, Random.SamplerType{UInt64}, Random.SamplerType{UInt8}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:29
[2] rand(::Random._GLOBAL_RNG, x::Union{Random.SamplerType{Int128}, Random.SamplerType{Int64}, Random.SamplerType{UInt128}, Random.SamplerType{UInt64}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:337
#...etc

This function allows us to see all of the functions matching the name we passed in.

It is also worth noting that the scope of this function can change depending on what packages you are using in the moment. See in the example below where I load in the POMDPs package and the number of available rand functions jumps significantly.

julia> using POMDPs

julia> methods(rand)
# 170 methods for generic function "rand":
[1] rand(rd::Random.RandomDevice, sp::Union{Random.SamplerType{Bool}, Random.SamplerType{Int128}, Random.SamplerType{Int16}, Random.SamplerType{Int32}, Random.SamplerType{Int64}, Random.SamplerType{Int8}, Random.SamplerType{UInt128}, Random.SamplerType{UInt16}, Random.SamplerType{UInt32}, Random.SamplerType{UInt64}, Random.SamplerType{UInt8}}) in Random at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/Random/src/RNGs.jl:29
#.. ETC. 

Read more about the methods function in the Julia Docs.

Upvotes: 0

Related Questions