At run time, I need to query for all loaded modules that call use MyUseModule. Say I have:
defmodule MyUseModule do
defmacro __using__(_) do
quote do
# stuff here
end
end
end
defmodule Example1 do
use MyUseModule
end
defmodule Example2 do
use MyUseModule
end
defmodule Example3 do
# This module does not use MyUseModule
end
defmodule MyUtil do
def find_modules_using(MyUseModule) do
# ???
end
end
Been working on it for over an hour. Can’t figure it out. Any help would be appreciated.
1 Like
Curious what your use case is, but this library could be a good starting point. It generates the AST for each file to analyze for dependencies among modules.
1 Like
I decided to go another route on this. I’ll just specify the target modules in a config setting.
I know it’s a bit late but I think you could use before_compile with a third module (not the one being called with use). Then in the defmacro for it you could set a module attribute that is configured to accumulate with the __CALLER__. I think this is somewhat similar to how protocols are able to get a list of their implementations but it does it with a second pass so as not to risk recompiling the whole app.
I agree though that using config is probably a better approach. Doing it this way could risk some pretty nasty cyclic compile dependencies that you would have a hard time undoing if this were to become a feature that you replied upon
I did something similar to what you suggested and kind of had it working. However, then I found a case that broke things. Then I realized that specifying Config entries would be much easier to implement and would make sense for my users as well. Thanks for sharing your perspective.
1 Like