Check if Elixir module exports a specific function
Building on the answer here and forum discussion here, there are a few ways to do it:
Check if Function exists for Module
You can check if the function name is present as a key in Map.__info__(:functions)
module = Map
func = :keys
Keyword.has_key?(module.__info__(:functions), func)
# => true
Check if Function exists with specific arity
To check with arity
, we can use Kernel.function_exported?/3
:
Kernel.function_exported?(Map, :keys, 1) # => true
Kernel.function_exported?(Map, :keys, 2) # => false
Updated to use function from the Kernel module instead of the :erlang module
Use Kernel.function_exported?/3
like this:
function_exported?(List, :to_string, 1)
# => true