Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show all methods of a function in Julia

Tags:

julia

How can I show all the methods of a function in Julia (multiple dispatch)?

For example, all the methods that exist in the namespace for the function abs.

like image 620
Georgery Avatar asked Mar 15 '20 15:03

Georgery


People also ask

Does Julia have methods?

Method TablesEvery function in Julia is a generic function. A generic function is conceptually a single function, but consists of many definitions, or methods. The methods of a generic function are stored in a method table.

What is :: In Julia?

A return type can be specified in the function declaration using the :: operator. This converts the return value to the specified type. This function will always return an Int8 regardless of the types of x and y .

What is multiple dispatch in Julia?

Using all of a function's arguments to choose which method should be invoked, rather than just the first, is known as multiple dispatch.

What is map in Julia?

The map() is an inbuilt function in julia which is used to transform the specified collection by using specified operation to each element.


1 Answers

The methods function will return the method table for the given function:

julia> methods(abs)
# 13 methods for generic function "abs":
[1] abs(a::Pkg.Resolve.FieldValue) in Pkg.Resolve at /home/david/pkg/julia-bin/julia-1.4.0-rc1/share/julia/stdlib/v1.4/Pkg/src/Resolve/fieldvalues.jl:61
[2] abs(a::Pkg.Resolve.VersionWeight) in Pkg.Resolve at /home/david/pkg/julia-bin/julia-1.4.0-rc1/share/julia/stdlib/v1.4/Pkg/src/Resolve/versionweights.jl:36
[3] abs(::Missing) in Base at missing.jl:100
[4] abs(x::Float64) in Base at float.jl:528
...

As of Julia 1.4, you can filter the method table by module. For example, listing the methods of abs which were defined in the Dates module:

julia> methods(abs, Dates)
# 1 method for generic function "abs":
[1] abs(a::T) where T<:Dates.Period in Dates at /home/david/pkg/julia-bin/julia-1.4.0-rc1/share/julia/stdlib/v1.4/Dates/src/periods.jl:95
like image 117
David Varela Avatar answered Sep 22 '22 21:09

David Varela