How do you check if an Elixir
module has exposed a specific public method? How do you check if the function has been exposed with a specific arity
?
Doesn't work:
Map.methods
Map.functions
Map.has_function(:keys)
Building on the answer here and forum discussion here, there are a few ways to do it:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With