:@which
@which
Applied to a function call, it evaluates the arguments to the specified function call, and returns the Method
object for the method that would be called for those arguments. Applied to a variable, it returns the module in which the variable was bound. It calls out to the which
function.
Examples
-
Find the specific method called for a function with given arguments:
julia> add(x::Int, y::Int) = x + y julia> add(x::Float64, y::Float64) = x + y julia> @which add(2, 3) add(x::Int64, y::Int64) in Main at REPL[1]:1
In this example,
@which
is used to determine the specific method that would be called whenadd(2, 3)
is executed. -
Identify the module where a variable is bound:
julia> module MyModule my_variable = 42 end julia> @which MyModule.my_variable MyModule
Here,
@which
is used to find the module where the variablemy_variable
is bound. - Handle cases where arguments match multiple methods:
julia> foo(x::Int) = x * 2 julia> foo(x::Float64) = x / 2 julia> @which foo(2.0) foo(x::Float64) in Main at REPL[2]:2
If multiple methods match the arguments,
@which
returns the most specific method.
Common mistake example:
julia> @which add(2.0, 3.0)
ERROR: MethodError: no method matching add(::Float64, ::Float64)
Closest candidates are:
add(::Int64, ::Int64) at REPL[1]:1
In this example, @which
throws a MethodError
because there is no method defined for add(::Float64, ::Float64)
. It's important to ensure that the function call has a matching method to avoid such errors. Always check the method signatures before using @which
.
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.