methodswith

methodswith(typ[, module or function][, showparents])

Return an array of methods with an argument of type typ. If optional showparents is true, also return arguments with a parent type of typ, excluding type Any.

The optional second argument restricts the search to a particular module or function.

Examples

julia> module MyModule
           struct MyType
               x::Int
           end

           myfunc(a::MyType, b::Int) = println("Method 1")
           myfunc(a::MyType, b::Float64) = println("Method 2")
           myfunc(a::Int, b::Float64) = println("Method 3")
       end
  1. Find methods with argument type:

    julia> methodswith(MyModule.MyType)
    2-element Array{Method,1}:
    myfunc(a::MyModule.MyType, b::Int64) in Main.MyModule at REPL[2]:5
    myfunc(a::MyModule.MyType, b::Float64) in Main.MyModule at REPL[2]:6

    This example returns an array of methods that have an argument of type MyModule.MyType.

  2. Find methods with parent type and exclude Any:

    julia> methodswith(MyModule.MyType, true)
    3-element Array{Method,1}:
    myfunc(a::MyModule.MyType, b::Int64) in Main.MyModule at REPL[2]:5
    myfunc(a::MyModule.MyType, b::Float64) in Main.MyModule at REPL[2]:6
    myfunc(a::Int64, b::Float64) in Main.MyModule at REPL[2]:7

    This example includes methods with a parent type of MyModule.MyType (excluding Any) in addition to the methods with the exact argument type.

  3. Restrict search to a specific module or function:
    julia> methodswith(MyModule.MyType, MyModule)
    2-element Array{Method,1}:
    myfunc(a::MyModule.MyType, b::Int64) in Main.MyModule at REPL[2]:5
    myfunc(a::MyModule.MyType, b::Float64) in Main.MyModule at REPL[2]:6

    This example restricts the search to methods defined within the MyModule module.

Common mistake example:

julia> methodswith(MyModule.MyType, MyModule.myfunc)
ERROR: TypeError: in Type, in parameter, expected Type, got typeof(myfunc)

In this example, the second argument is passed as MyModule.myfunc which is not a valid input. The second argument of methodswith should be a module or function name (as a symbol).

See Also

assert, backtrace, code_llvm, code_lowered, code_native, code_typed, code_warntype, :@which, compilecache, current_module, eval, finalize, finalizer, fullname, function_module, function_name, include_dependency, InterruptException, invoke, isconst, isdefined, isgeneric, methodswith, method_exists, module_name, module_parent, require, subtypes, unsafe_load, workspace, __precompile__,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: