What could be the shortest possible way to get a list of all the modules implementing a behaviour?
e.g.
defmodule X do
@callback foo()
end
defmodule A do
@behaviour X
def foo() do
"hello"
end
end
defmodule B do
@behaviour X
def foo() do
"world"
end
end
I would like to get a list as [A,B]
. I tried loading all the modules and then filtering using the key attribute behaviour
but I think it's going to be very slow. Is there any other way?
No, there is no better way. Behaviours are not indexed in any way, as they are a compile-time feature used to check that all required callback functions have been defined. So something like this is as good as it gets:
for {module, _} <- :code.all_loaded(),
X in (module.module_info(:attributes)[:behaviour] || []) do
module
end