function_module
function_module(f::Function, types) -> Module
Determine the module containing a given definition of a generic function.
Examples
julia> function_module(f::Function, types)
m = @which f(types)
return parentmodule(m)
end
Examples:
-
Find the module of a built-in function:
julia> module_of_sin = function_module(sin, (Float64,)) MainIn this example, we use the
function_modulefunction to determine the module of thesinfunction for theFloat64type. - Find the module of a user-defined function:
julia> function greet(name) println("Hello, $name!") end
julia> module_of_greet = function_module(greet, (String,)) Main
Here, we define a simple function `greet` and use `function_module` to find its module for the `String` argument type.
3. **Handling generic functions with multiple methods:**
```julia
julia> function add_numbers(a::Int, b::Int)
return a + b
end
julia> function add_numbers(a::Float64, b::Float64)
return a + b
end
julia> module_of_add = function_module(add_numbers, (Int, Int))
Main
In this example, we have a generic function add_numbers with multiple methods. We use function_module to find the module of the specific method that matches the input types (Int, Int).
Common mistake example:
julia> module_of_abs = function_module(abs, (String,))
ERROR: MethodError: no method matching abs(::Type{String})
Here, the mistake is providing the argument type (String,) which does not match any method of the abs function. Make sure to provide the correct argument types for the function you are querying.
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.