How to determine from which module a specific function was imported in the elixir

When several external modules are enabled by using use on some intermediate module, is there an easy way to determine in which module this method is defined?

eg:

 defmodule ModuleB do def method_b do end end defmodule ModuleA do # imports ModuleB implicitly use SomeModuleImportingModuleB def method_a # how to determine this is ModuleB.method_b? method_b end end 
+4
source share
2 answers

I found a solution that works for me, capturing the function with & , and then checking it:

 def method_a IO.inspect &method_b/0 # outputs &ModuleB.method_b method_b end 
+3
source

Each module defines a __info__ function, you can use it to view the functions exported by this module:

 IO.inspect ModuleB.__info__(:exports) # => [method_b: 0, module_info: 0, module_info: 1, __info__: 1] 

Please note that when using use , the module in question can enter code directly into the module being defined and create functions dynamically - this can lead to functions becoming available that were not defined in the used module.

+2
source

Source: https://habr.com/ru/post/1238462/


All Articles